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

    Join Date
    Jul 2017
    Posts
    2
    Rep Power
    0

    Match all strings between same tag


    Hi folks,
    I need some help with my regex expression. (Language C#)

    I want to match/group the following string
    Code:
    C: My Comment T: My Type P: My Param
    into something like this:
    Code:
    C => My Comment
    T => My Type
    P => My Param
    So, the single letter is somehow a key and the rest is the value.

    My current expr looks like this
    Code:
    @"(.): (.*?)(.: |\z)"
    But the second one, so "T: My Type" is not getting matched.

    I would also appreciate it, if you can explain your changes I'm quite new to regex

    Thx in advance
  2. #2
  3. Maddening Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,453
    Rep Power
    9645
    Normally any part of a string that gets used when matching will not be available for the next match: that
    Code:
    (.: |\z)
    will match the "T: ", meaning the second attempt starts at "My Type" and so has to skip ahead to the P: to match.

    A positive lookahead works differently. It's like regular matching except it "looks" at part of the string without "using" it. Works like
    Code:
    (?=.: |\z)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2017
    Posts
    2
    Rep Power
    0
    Originally Posted by requinix
    Normally any part of a string that gets used when matching will not be available for the next match: that
    Code:
    (.: |\z)
    will match the "T: ", meaning the second attempt starts at "My Type" and so has to skip ahead to the P: to match.
    yeah, I already thought so. Simple wasn't sure how to change this behaviour

    Originally Posted by requinix
    A positive lookahead works differently. It's like regular matching except it "looks" at part of the string without "using" it. Works like
    Code:
    (?=.: |\z)
    Works like a charm, thank you

IMN logo majestic logo threadwatch logo seochat tools logo