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

Matching percent sign except when preceded by numbers

Last post 05-17-2012, 7:02 PM by Aussie Susan. 2 replies.
Sort Posts: Previous Next
  •  05-17-2012, 10:05 AM 85199

    Matching percent sign except when preceded by numbers

    Hello,

    I am trying to write a RE that will match a percent sign (%) in a name field (care-of indicator) except when preceded by numbers as expressed as a percentage. For example

    Gerald Duncan % Karen Duncan          ==> positive match

    Gerald Duncan 35% int Karen Duncan ==> negative match

    The desired result is to split the name field on the % sign.

    The following RE excludes matching when the % is part of a percentage but doesn't match when preceded by non-numerals.  

    \b{\D}*\W*%\W*\b

    Your help as always would be much appreciated. Thank you,

    Gerald

  •  05-17-2012, 5:30 PM 85209 in reply to 85199

    Re: Matching percent sign except when preceded by numbers

    what is your regex flavor? I can try to help with .NET and Oracle if you happen to write in those flavors.

     

  •  05-17-2012, 7:02 PM 85212 in reply to 85199

    Re: Matching percent sign except when preceded by numbers

    As Sergei has asked, the key to this is probably the regex variant that you are using.

    I must admit that I'm not familiar with any regex variant that would accept the '{\D}*' part of your pattern except perhaps to treat the '{' and '}' as literal characters and thos would make your pattern not match any of your examples.

    IF your regex variant understands negative lookbehinds, then

    (?<!\d)%

    would be a simplistic pattern that would do as you ask. However I suspect from your pattern that there are other criteria that are involved with the match (otherwise why would you have the '\b' and '\W' parts that do not get mentioned in the "% but not preceded by a digit" (my paraphrase of your question) statement of your issue.

    By the way, my pattern above does work in the "split" operation within my regex tester as well but will leave the leading and trailing whitespace etc that you may be trying to get rid of as well. Just guessing....

    Susan

View as RSS news feed in XML