V_H24
V_H24

Reputation: 139

Regular Expression for phrases starting with TO

I am pretty new to Regular Expression. I want to write a regular expression to get the TO Followed by the rest of it after each new line. I tried to use this but doesn't work properly. ^TO\n?\s?[A-Za-z0-9]\n?[A-Za-z0-9] It only highlights properly the TO W11 which all are in one line. Highlights only TO from first data and the 3rd data only highlights the first line. Basically it doesn't read the new lines. Some of my data looks like this:

 TO
 EXTERNAL
 TRAVERSE

 TO W11

 TO CONTROL
 TRAVERSE

I would appreciate if anybody can help me.

Upvotes: 0

Views: 102

Answers (2)

ToastyMallows
ToastyMallows

Reputation: 4273

It looks like your pattern isn't matching because the start of the string is really a space and not the T character. Also, [A-Za-z0-9] matches only one character, and you want the whole word. I used the + to denote that I want one or more matches of those characters.

(TO\n?\s?[A-Za-z0-9]+)

This regex matches "TO EXTERNAL", "TO W11" and "TO CONTROL". Be sure to use the global modifier so that you get all matches, not just the first one.

Upvotes: 0

John Gibb
John Gibb

Reputation: 10773

Make sure you use a multiline regex:

var options = RegexOptions.MultiLine;

foreach (Match match in Regex.Matches(input, pattern, options))
...

More at: http://msdn.microsoft.com/en-us/library/yd1hzczs(v=vs.110).aspx

Upvotes: 1

Related Questions