#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    What is wrong with my RegEx?


    Hi there
    I've just started with C# regex and cannot figure out why I cannot find the firts end of line in the follwoing source

    "-- returns Min value of integer parameters \r\n returns Integer\r\n fgdfgdfgdf \r\n dgdfgdg"

    My pattern is

    new Regex(String.Concat(@"\-\-[A-z\s]+(?=\n)"), RegexOptions.Multiline).Matches(s)[0]

    i.e. "Find it started with --, followed by any sequence of words, follwoed by a CR"

    And it returns me

    "-- returns Min value of integer parameters returns Integer fgdfgdfgdf"

    Why not ""-- returns Min value of integer parameters " only?

    Thanks.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    because \s matches newline characters as well, and the + quantifier is greedy, that is, it matches as many characters a possible.

    If you only want to match spaces, you have to specify that: [ ]

    Matching before a \n character is also problematic, because it may cut off the string after the \r, leaving you with a nonsense character. Instead, use the $ anchor to match the ending of a line. You may have to turn off multiline mode (I'm not sure why you turned in on in the first place).
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0
    Thanks, I will try that

IMN logo majestic logo threadwatch logo seochat tools logo