Regex problem

themster

Newcomer
Joined
Oct 29, 2003
Messages
10
I'm currently working on a modul for a crypto tool and I'm having some problems. The modul is supposed to be able to match word by the use of a mask consisting of letters. For exemple should the mask 'abb' match the words 't ree' and 's upp ose'.

So far I have written a parser that generates a regex patttern. The mask 'abba' becomes the pattern '(\w)(\w)\2\1'. So far so good.

The problem arises when I use a mask similar to the following, 'abcd'. This mask matches 'cats' but also 'room' because of the fact that '\w' matches any letter. In other words, I want that two letters in the mask are two different letters in the match. For exemple can't both 'b' and 'c' match 'o' (see the 'room' example).

I have tried to solve the problem by using this pattern instead, '(\w)([^\1])([^\1\2])([^\1\2\3])'. The problem is that regex doesn't seem to be able to replace the '\1' with the first group ('(\w)') when the '\1' is inside '[]'.

Is there any to solve this problem? I haven't worked much with regex so it's possible that there are functions within regex which will solve the problem.
 
Back
Top