KDecker
KDecker

Reputation: 7128

Regex matches for single line of text, but not when multiple lines of text are matched against?

I have some regex that is used with a FastColoredTextBox control to identify text that needs to have its TextStyle set upon TextChanged events. I am specifically having trouble identifying an issue with this regex.

@"(^STEP\s|^\s+STEP\s)(.*)$"

In my language I have a command STEP which has one argument, the argument is a valid String (the step name). In my editor and parser the command can only be parsed if it is on its own line with 0-N white space characters preceding it, followed by a single space, followed by the valid string, followed by the end of the line.

My issue is that the above regex does not match all instances, which have been hard to define (regex is confusing). For instance pasting the following text into my editor results in the step name being properly highlighted

STEP My Special Step Name 

Thought this text results in it not being highlighted

STEP My Special Step Name 
    // Comment

Along with this

//Comment
STEP My Special Step Name 

From what I can tell if the text I am trying to match against contains a newline it does not match. I have to assume that my use of the anchors(?) ^ and $ are the issue but I do not see how if they match the start end of of lines?

I originally started the regex trying to match newline characters but it quickly proved to be out of my grasp. I stumbled upon the ^ and $ during it and decided that they would work well, though I guess I was mistaken?

Upvotes: 2

Views: 2349

Answers (1)

l'L'l
l'L'l

Reputation: 47169

Try using Multiline Mode with your pattern:

(?m)(^STEP\s|^\s+STEP\s)(.*)$

MSDN RegEx Multiline Reference

Upvotes: 1

Related Questions