1

Regex Pattern: ((?:(?!.*(?:Jr|Sr|MD|I{2,3}|IV)$).?)(?:(?:\w\D[^&%,.]+)\s?)+)(?:(Jr|Sr|MD|II{2,3}|IV).?)?

Input Text: Dela Cruz III

Expected Output: Group #1: Dela Cruz Group #2: III

Actual Output: Group #1: Pattern does not match since it detected the III and disregarded the whole string

Goal: I want my regex pattern parse the String and separate the Last Name from the Post fix (if any).

What seems to be wrong with the pattern?

1 Answers1

0

Lookaheads are about what immediately follows the preceding character, class, or capture group. This small addition makes this work on this one string, and should work on general single name stylings, but we'll need more examples if your goal is extraction rather than validating a single name.

Removing the $ may be more akin to the solution you want, it depends if these names are the whole string or not.

((?:(?!.*(?:Jr|Sr|MD|I{2,3}|IV)$)\.?)(?:(?:\w\D[^&%,.]+)\s?)+)

Are you doing validation, or extraction? WHat I've posted is more of a validation pattern.

Regular Jo
  • 5,190
  • 3
  • 25
  • 47
  • Hi! Your pattern worked to check if string has Jr, Sr MD etc. However what I want to achieve is to capture only 'Test 1 Test2' and not disregard the whole string. – Romiazon Salazar Mar 02 '15 at 14:22
  • @RomiazonSalazar Can you edit your question with some actual sample strings? Are these individual names in each string or is it many names in one string? – Regular Jo Mar 02 '15 at 19:06
  • I have edited the questions. I'm actually parsing the name. I have pasted the Regex Pattern from Last Name to Postfix since I am having a problem mainly from that part – Romiazon Salazar Mar 03 '15 at 04:24
  • @RomiazonSalazar Try this `^((\b(?!(?:Jr|Sr|MD|I{2,3})\b)[a-z]+\b ?)+)`, or you may prefer to remove the leading carrot. It will permit things like `Jrs`, `MDS`, etc. If you don't want it to, remove the `\b` after the `{2,3}` – Regular Jo Mar 03 '15 at 05:32
  • I changed the pattern to ^(?:(?:\b(?!(?:Jr|Sr|MD|I{2,3})\b)\w\S+[^&%,.]\b ?)+) so that it will also accept special characters except &%, and . Would that be okay? – Romiazon Salazar Mar 03 '15 at 11:59
  • @RomiazonSalazar If it's working for you than it's okay :) You can test it at regexr.com and try to trip it, don't forget to set the flags options on the right. turn on all 3 (igm) for testing. – Regular Jo Mar 03 '15 at 18:41
  • Thank you very much! Hmm. Could you please give me some recommended tutorials online about regex? It would really help me deepen my understanding regarding regex :) – Romiazon Salazar Mar 04 '15 at 11:09
  • @RomiazonSalazar regular-expressions.info and a lot of fidding in http://www.regexr.com or http://www.regex101.com – Regular Jo Mar 04 '15 at 20:38
  • There are also great software solutions like RegexBuddy, Expresso, and also RegexHero (website) but all of which have a (fair) price. Honestly the biggest downside of Regexr and Regex101 is that they're limited to javascript's implementation of regex, so while they recognize negative lookbehinds, they cannot 'use' them so testing negative lookbehinds is impossible on those. – Regular Jo Mar 04 '15 at 20:45