Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to completely ignore linebreak and tab in RegEx?

Tags:

c#

regex

asp.net

Is there any way to completely ignore line break and tab characters etc. in RegEx? For instance, the line break and tab characters could be found anywhere and in any order in the content string.

... [CustomToken \t \r\n Type="User" \t \r\n Property="FirstName" \n /] ... [CT ...

The is the RegularExpression that I am currently using:

(\[CustomToken).*?(\/\])

.NET API

Regex.Matches(string input, string pattern)

Thanks for your suggestion.

like image 706
Thurein Avatar asked Jan 11 '11 03:01

Thurein


2 Answers

If you just want that regex to match that input, all you need to do is specify Singleline mode:

Regex.Matches(input, @"\[CustomToken).*?(/\])", RegexOptions.Singleline);

The dot metacharacter normally matches any character except linefeed (\n). Singleline mode, also known as "dot-matches-all" or "DOTALL" mode, allows it to match linefeeds as well.

like image 154
Alan Moore Avatar answered Sep 29 '22 03:09

Alan Moore


There is no way to "ignore" any type of character with regex. You can ignore letter case, but that's about it.

Your best bet is to use \s+ where you would expect some type of whitespace. The \s class will match any whitespace, including newlines, carriage returns, tabs, and spaces, and this will make your regex pattern look a lot nicer.

like image 29
Jake Avatar answered Sep 29 '22 02:09

Jake