How to completely ignore linebreak and tab in RegEx?

后端 未结 4 650
-上瘾入骨i
-上瘾入骨i 2020-12-18 22:54

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

相关标签:
4条回答
  • 2020-12-18 23:09

    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.

    0 讨论(0)
  • 2020-12-18 23:14

    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>)
    
    0 讨论(0)
  • 2020-12-18 23:20

    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.

    0 讨论(0)
  • 2020-12-18 23:31

    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
    
    0 讨论(0)
提交回复
热议问题