tags:

views:

418

answers:

2

(I'm escaping all my quotes, which may make it difficult to read)

I need to match a string that begins and ends with the same character in flex...I know the long hand way (RE being - \"a[^(a\")]a\" | \"b[^(b\")b\" | etc...), but I'm positive this isn't what I'm required to do (midterm tomorrow!);

I need to do this in flex, but if you can think of a short regex for it, I may be able to convert it to flex notation.

What I was thinking of was something along the lines of -

%%
int firstChar;
%x string;
%%
\"[A-Za-z] { firstChar = yytext+1; /* to get first character,
                                      for people unfamiliar
                                      with c pointers */
    BEGIN(string);}
<string>[^((firstChar)\")] {}
<string>[(firstChar)\"] { BEGIN(INITIAL); }

(new to flex, may be incorrect notation)

But this bugs me in a few ways, first, having that variable makes this not a regular language; second, I don't know if you can even use a variable to in a pattern match; and third, I don't know how NOT to match it, if it's just a normal string. And third, I don't know how to return everything that's been matched while in 'string'

Thanks for your help!

A: 

(I'm escaping all my quotes, which may make it difficult to read)

I need to match a string that begins and ends with the same character in flex...

Try something like this:

^(?s)(.).*\1$

Which seems to work for Flex' regex according this Flex regex tester with the string

she sells seashells 
by the seashores

(starts and ends with an s and has a line break in it)

Bart Kiers
A: 

Use backreference. \1 to \9 refer to the first nine groups captured by the regex using parentheses ().

var regex:RegExp = /\b([a-zA-Z])\w+\1\b/g;
var test:String = "some text that starts and ends with same letter";
var result:Object;
while(result = regex.exec(test))
    trace(result[0]);

traces

text
that
starts

Regex explained:

\b          //word boundary
([a-zA-Z])  //capture the first character
\w+         //one or more characters (use * to capture two letter words)
\1          //the first character
\b          //word boundary

g           /*
              make the regex global. Without this, the program will will 
              print the first match ('text') in an infinite loop
            */
Amarghosh