Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Need help with zero or once matches

Last post 02-27-2008, 11:11 AM by Bkr. 2 replies.
Sort Posts: Previous Next
  •  02-26-2008, 8:17 AM 39869

    Need help with zero or once matches

    Urls:
     "~/Home/Partner/OnBoard/Cabin/default.aspx"
    "~/Home/Partner/OnBoard/Cabin/default.aspx?param=2"

    regex:
    (~/[Hh]ome/[Pp]artner/[Oo]nboard/)(.*)(/.+\..+)(\?)(.*)


    current matches:
     "~/Home/Partner/OnBoard/Cabin/default.aspx"
    None

    "~/Home/Partner/OnBoard/Cabin/default.aspx?param=2"
    1: ~/Home/Partner/OnBoard/
    2: Cabin
    3: /default.aspx
    4: ?
    5: param=2

    wish:
     "~/Home/Partner/OnBoard/Cabin/default.aspx"
    5 matches where match 4 and 5 is null

    "~/Home/Partner/OnBoard/Cabin/default.aspx?param=2"
    As it is.



    I could just use 2 regex but I don't like that solution.

    Kin regards
    Bkr




     

     

  •  02-26-2008, 6:04 PM 39893 in reply to 39869

    Re: Need help with zero or once matches

    Try:

    (~/home/partner/onboard/)(.*)(/.+\.[^?]+)((\?)(.*))?

    with the 'ignore case' option on.

    As you presented your pattern, you would not get a match without the 'ignore case' because the pattern was looking for the literal text "nboard" but the examples had the 'B' capitalised. If you were using 'ignore case' then you don't need the "[Oo]" subpattern.

    To make the last part optional, I have added another set of parentheses to make it "((\?)(.*))?". This means that the whole '?param=2' part can either be there or missing. Note that you now have 6 match groups and that you want to look at match groups #5 and #6 instead of #4 and #5 as before. (Of course you can use the fact that #4 will still be null if both #5 and #6 are null)

    HOWEVER, because it can be missing and just before it you had '.+' in the pattern, this was grabbing everything to the end of the string in all cases and always leaving the (now) optional part unused. Your original pattern relied on backtracking when looking for the ?, but it is now optional and so cannot be relied on. There are two solutions to this, one of which I have presented above - scan forward accepting everything EXCEPT a ?; if you find one then the optional subpattern that follows will be used, if not then you must have found the end of the string/line (depending on the multiline option setting) and the optional tailer will be skipped.

    The other way is to use a non-greedy option:

    (~/home/partner/onboard/)(.*)(/.+\..+?)((\?)(.*))?

    but you have not mentioned the regex/programming language you are using and so I have no idea if this is possible.

    Susan

     

  •  02-27-2008, 11:11 AM 39919 in reply to 39893

    Re: Need help with zero or once matches

    Thx it works ! .... im coding in C# .NET
View as RSS news feed in XML