Reputation: 2566
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.
Upvotes: 14
Views: 34097
Reputation: 4268
do you need the tab/newline? You could always just replace the tab/newline character with an empty character to remove them.
string mystring = "\t\nhi\t\n";
string mystring_notabs = mystring.Replace("\t",""); //remove tabs
mystring = mystring_notabs.Replace("\n",""); //remove newline and copy back to original
Upvotes: 1
Reputation: 271
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>)
Upvotes: 0
Reputation: 75222
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.
Upvotes: 25
Reputation: 7773
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.
Upvotes: 4