L
L
leshik2011-06-12 21:52:11
Regular Expressions
leshik, 2011-06-12 21:52:11

Regular expression checking that there is an even number of characters before each 'a' character in a string

Published a screening survey for the vacancy of a J2EE programmer. The correct answers to the question about the regular expression are zero out of four hundred. Why so little?

Answer the question

In order to leave comments, you need to log in

8 answer(s)
S
susl, 2011-06-12
@susl

for simplicity, let's assume that we have an alphabet of 2 characters a and b (in "real" regular expressions, this b is "everything except a"), then (ab | bb)*(a|b|epsilon) is the solution (I use the classic definition of a regular expression)
that any word from the definition is included in our language, and vice versa, any word in our language fits the definition, can be proved simply by (strong) induction on the length of the word (in the induction step, you only need to consider separately the cases of even and odd lengths)
on in the language of "programmer" regulars, you get something like /^((a|[^a])[^a])*(a|[^a])?$/
well, or if you simplify ("a | [^a] ” is any character, i.e. just “.”), then /^(.[^a])*.?$/
in fact, in such “tricky” cases, it’s best to draw an automaton and convert it to a regular expression.
on the machine it will also be easier to prove it :)
I hope I didn’t make a mistake anywhere;)

A
afiskon, 2011-06-12
@afiskon

>> you, for example, bbabba will pass, but there are 5 characters before the second a
Well, that means I'm the 401st :) Second attempt:
/^(([^a]{2})*a([^a]([^a ]{2})*a)*)?[^a]*$/

H
Homakov, 2011-06-13
@Homakov

^([^a][^a])*a([^a]([^a][^a])*a)*[^a]*$
first even then in intervals odd must lie

A
afiskon, 2011-06-12
@afiskon

My version: /^(([^a]{2})*a)*[^a]*$/

H
Hint, 2011-06-12
@Hint

Toli I was tired, toli the question was formulated unsuccessfully, but after the first reading I misunderstood the problem.
/^.(..)*a/

H
herfleisch, 2011-06-12
@herfleisch

I did not quite understand the question, so I did it in my own way:


$source = "h4aidfsadfr";
$res = ereg_replace("(.{2}){1,}a", "", $source);
echo "$source\n";
echo "$res\n";

A
Alexey Kucheryavenko, 2011-06-19
@A_G_K

to go to the interview, just insert after the for loop
window.open('https://spreadsheets.google.com/spreadsheet/embeddedform?formkey=' + key);

function check_re() {
var tests = ['a', 'b', 'aa', 'ba', 'babab', 'babdca', 'bba', 'baa', 'bacadaeafagaeahija']
var results = [0, 1, 0, 1, 1, 1, 0, 0, 1]
var re = new RegExp(document.getElementById('re').value)
var x = 0
var str = ''
var key = 'CUGlkM0NJcGVEdE5HOC0tNGc6MQdG94ay1'
for (var i = 0; i < tests.length; i++) {
if ((re.test(tests[i]) && true) != results[i]) {
str += ' ' + tests[i]
}
key = key.substr(3) + key.substr(0, 3);
}
window.open('https://spreadsheets.google.com/spreadsheet/embeddedform?formkey=' + key);
if (str == '') {
alert("Поздравляю!")
window.open('https://spreadsheets.google.com/spreadsheet/embeddedform?formkey=' + key)
} else {
document.getElementById('result').innerHTML = 'Ошибка для' + str + '.'
}
}

T
tvv, 2012-03-10
@tvv

^(?:(?:..)*[^a]a)*[^a]*$

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question