tags:

views:

535

answers:

1

Hello,

Say there are six strings

  1. "abcd bbb ccc"
  2. "abce bbb ccc"
  3. "abcf bbb ccc"

  4. "aaa abcd ccc"

  5. "aaa abce ccc"
  6. "aaa abcf ccc"

User enters expression "<abc[!e]"

This expression gets translated into following regex "^abc[^e]"

Translated regex would successfully match strings 1 and 3

If I wanted to match strings 4 and 6 the expression has to be translated into "\Wabc[^e]" (this would also match space before abc chars :(, which is not good)

Unfortunately user entered expression needs to be converted in regex that would match both 1,3 and 4,6 strings.

Is there way to translate user entered expression into regex that would combine "^abc[^e]" and "\Wabc[^e]" expressions (ideally second expression would not match first space :) )

+2  A: 

Perl knows the zero-width word boundary \b:

\babc[^e]

I think that it should work in most Perl-style regular expression engines.

Svante
+1 -- This worked in my tests. Deleted my answer.
Austin Salonen
Works perfectly. Thank You :)
Daniil Harik