0

Any experts on Regex, that could potentially find a pattern on this data, im looking for one that will match exactly, down to spaces and commas and dashes. Here is the sample data of what i need to match:


word word, alphanumeric-PRT-word-number

word word, alphanumeric-PRT-number


-word: any size word

-alphanumeric: 3 letters and up to 2 numbers, so XXX# or XXX##

-number: up to 3 digits, so # or ## or ###

-PRT: is the only static value here

NOTE: no other punctuation other than the spaces, comma and dashes where they are.


So far have something close to it but rather clunky and it doesnt cover all bases, i built it here: http://buildregex.com/ using their logic and it kinda works:

/(?:[^_\ ]+)(?:\ )(?:[^_\ ]+), (?:[^_\ ]+)-PRT-(?:[^_\ ]*)/gi

If any can assist in refining this that will be welcome

https://regex101.com/r/8cc52u/2

Thanks a lot

revo
  • 43,830
  • 14
  • 67
  • 109
Alejandro Suarez
  • 129
  • 1
  • 12
  • 1
    You don't need experts but [*What does this regex mean?*](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – revo Jul 07 '17 at 05:36
  • 2
    Possible duplicate of [Reference - What does this regex mean?](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – revo Jul 07 '17 at 05:36
  • What is a word for you? Only letters from `[a-zA-Z]` or with diacritics `éèçàÉÀÖù...`? Are dash allowed? or apostroph? – Toto Jul 07 '17 at 07:59
  • @revo although rather usefull as reference...the data there is cumbersome for begginers. I totally agree with the givethecodez issue...but not everyone is looking for that. I had attempted kn my own before and now thanks to the answer i was able to piece together some of the concepts to do other regex expressions. Thanks for assisting there too i saw your remarks on the slected answer. Cheers – Alejandro Suarez Jul 09 '17 at 03:52

1 Answers1

1

Here's one way to do it:

/^[a-z]+\s[a-z]+,\s[a-z]{3}\d{1,2}-prt-([a-z]+-){0,1}\d{1,3}$/gi
  • ^: start of line
  • [a-z]+: one or more letters
  • \s: any space character
  • [a-z]+: one or more letters
  • ,: ,
  • \s: any space character
  • [a-z]{3}: three letters
  • \d{1,2}: one or two digits
  • -prt-: -prt-
  • ([a-z]+-){0,1}: one or more letters followed by -, zero or one time
  • \d{1,3}: one, two or three digits
  • $: end of line

Example: https://regex101.com/r/BhS8kM/5

Or, as suggested by revo:

/^[a-z]+ [a-z]+, [a-z]{3}\d{1,2}-prt-([a-z]+-)?\d{1,3}$/gi

Example: https://regex101.com/r/BhS8kM/7

Gerry
  • 9,689
  • 3
  • 28
  • 37
  • This also matches `word word, wrd12-prt--5` – revo Jul 07 '17 at 06:09
  • @revo Nice catch, thanks! I've updated answer to cover that scenario. – Gerry Jul 07 '17 at 06:18
  • 2
    GJ FYI all `{0,1}` range could be converted to `?` which has same meaning. I believe also `\s` isn't suitable as being not strict in catching [Space 0x20](http://www.fileformat.info/info/unicode/char/0020/index.htm). Use space character alone. – revo Jul 07 '17 at 06:24
  • Thats simply genius no only that your explanation help me understand and do another comple regex...grateful forever. :) – Alejandro Suarez Jul 09 '17 at 03:46