How to completely ignore linebreak and tab in RegEx?

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 rn Type="User" t rn 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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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.

Method 3

do you need the tab/newline? You could always just replace the tab/newline character with an empty character to remove them.

string mystring = "tnhitn";

string mystring_notabs = mystring.Replace("t",""); //remove tabs

mystring = mystring_notabs.Replace("n",""); //remove newline and copy back to original

Method 4

I had an issue with a multi-line XML value. I wanted the data within a description field, and I did not want to change my C# code to use the single line option, as I was dynamically reading regular expressions from a database for parsing. This solved my issue, particularly the (?s) at the front:

 (?s)(?<=<description>).*(?=</description>)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x