Page 1 of 1

More regular expression help

Posted: Mon May 04, 2009 9:36 pm
by barry.marcus
Task 1: I'm looking to match, using rex, whenever the first "visible" character of a string is a plus sign. That is, the "+" could be the very first character, or it could be preceeded by any number of spaces, tabs, carriage returns, etc.

Task 2 is similar, but I want to match if the first visible character is NOT a plus sign. Again, in this case, I don't care about any characters such as spaces, tabs, CR, etc., at the beginning.

Thanks again for the help.

More regular expression help

Posted: Tue May 05, 2009 10:06 am
by jason112
task 1: >>=\space*\+=
task 2: >>=\space*!\+=

Discussing the components:
>>= Two greater than symbols is the "anchor" sequence, and anchoring to an empty sequence (just the equals) at the beginning or end of the expression matches the beginning or end of the string. More built-in sequences, such as \alpha, \alnum, etc., are listed at http://www.thunderstone.com/site/vortex ... yntax.html

\space* The "\space" sequence is the C language character class, which includes space, tab, CR, LF, etc. The * means match 0 or more.

\+= Match exactly one plus. Plus is normally special (means "one or more of the previous set), so it must be escaped by a backslash when we want to match it literally.

!\+= Match exactly one character that ISN'T a plus

More regular expression help

Posted: Tue May 05, 2009 10:49 am
by mark
task 2 could also be >>=\space*[^+]=

More regular expression help

Posted: Tue May 05, 2009 1:03 pm
by barry.marcus
Thank you very much! I appreciate the help.