none
RegEx for matching specific format RRS feed

Answers

  • OK, I'm trying to grind through this with you.

    Here's what I have so far.  Seems to work well.

    ^
    (?>
    (?<open>(\w+@)?\w+\()
    |
    (?<-open>\))
    |
    \w+
    |
    ,(?=\s*\w)
    |
    \s*
    )
    *
    (?(open)(?!))
    $

    My explanation:

    ^ and $ match beginning and end of string respectively.

    (?> ... ) is greedy matching.

    (?<open> ...) creates a capture called open with the given open token ...

    (?<-open> ...) closes a capture called open with the given closing token ...

    (?= ... ) is a zero-width positive lookahead assertion.  Only matches if ... follows

    (?(open)(?!)) is an assertion that there are no open captures (balances parentheses).

    So the overall pattern looks for either an open delimiter of:

    foo@bar(

    where the (foo@) part is optional

    or a close delimiter of

    )

    or some word chars

    or a comma followed by at least one word character (and optional whitespace)

    in C#:

    string pattern = @"^
    (?>
    (?<open>(\w+@)?\w+\()
    |
    (?<-open>\))
    |
    \w+
    |
    ,(?=\s*\w)
    |
    \s*
    )
    *
    (?(open)(?!))
    $";
                Regex regex = new Regex( pattern, RegexOptions.IgnorePatternWhitespace );



    • Edited by Wyck Tuesday, May 14, 2013 4:21 PM clarification about balancing assertion.
    • Marked as answer by Ram Kinkar Pandey Tuesday, May 14, 2013 6:26 PM
    Tuesday, May 14, 2013 4:19 PM

All replies

  • You need to investigate "balancing groups".  You need balancing groups to match parentheses at arbitrary nesting depths.  But you also need some extra mojo to do the comma separated list and function name prefixes...as well as optional missing "Something" before "@FunctionName", and naked parameters.

    Do you also intend to allow other expressions?  Are the ellipses literal or just for illustration? And do you want to allow no parameters?  like FunctionName() 

    For example, to get you thinking in the right direction, here's a regex that matches parentheses only:

    \((?>[^()]+|\((?<open>)|\)(?<-open>))*(?(open)(?!))\)

    This isn't exactly what you want, but I'll have to get back to you.  Or maybe someone else can figure it out.

    Tuesday, May 14, 2013 3:03 PM
  • Wyck,

    Thanks for your response . Ellipse used "." is not literal, that just meant that I can have more such type of repeating pattern. In this case I can have any number of parameters.

    For example I can have

    Somthing@Function() with no parameter, can have it with several parameters and as parameter I can also have Something@Function() and so on. Where "Something" (which is a string with no spaces) is optional.

    I am starting with given regex. In case you can advise me something based on my response, it will be much useful. I will also look into balancing groups.

    regards,


    Software Engineer

    Tuesday, May 14, 2013 3:15 PM
  • OK, I'm trying to grind through this with you.

    Here's what I have so far.  Seems to work well.

    ^
    (?>
    (?<open>(\w+@)?\w+\()
    |
    (?<-open>\))
    |
    \w+
    |
    ,(?=\s*\w)
    |
    \s*
    )
    *
    (?(open)(?!))
    $

    My explanation:

    ^ and $ match beginning and end of string respectively.

    (?> ... ) is greedy matching.

    (?<open> ...) creates a capture called open with the given open token ...

    (?<-open> ...) closes a capture called open with the given closing token ...

    (?= ... ) is a zero-width positive lookahead assertion.  Only matches if ... follows

    (?(open)(?!)) is an assertion that there are no open captures (balances parentheses).

    So the overall pattern looks for either an open delimiter of:

    foo@bar(

    where the (foo@) part is optional

    or a close delimiter of

    )

    or some word chars

    or a comma followed by at least one word character (and optional whitespace)

    in C#:

    string pattern = @"^
    (?>
    (?<open>(\w+@)?\w+\()
    |
    (?<-open>\))
    |
    \w+
    |
    ,(?=\s*\w)
    |
    \s*
    )
    *
    (?(open)(?!))
    $";
                Regex regex = new Regex( pattern, RegexOptions.IgnorePatternWhitespace );



    • Edited by Wyck Tuesday, May 14, 2013 4:21 PM clarification about balancing assertion.
    • Marked as answer by Ram Kinkar Pandey Tuesday, May 14, 2013 6:26 PM
    Tuesday, May 14, 2013 4:19 PM