Search for a specific regular expression

I was looking to create a regex for my specific situation. The farthest I came with my own limited regular expression knowledge and by searching StackOverflow is Regex:

^[pP][a-zA-Z0-9- ]* 

I am looking for a regex that forces a string:

  • start with the letter "p" or "P", so lower and upper case
  • no longer than 19 characters
  • may contain '-', '% 20', '' or '+'
  • end with '0000'
  • be alphanumeric between "p" and four zeros and separators "-", "% 20", "or" + "

Examples of strings that must match a regular expression:

  • PLX000000000PQ50000
  • pLX000000000PQ50000
  • plx000000000pq50000
  • PLX-0000-0000-0PQ5-0000
  • PLX-0000-0000-0PQ50000
  • PLX + 0000 + 0000 + 0PQ5 + 0000
  • PLX% 200000% 200000% 200PQ5% 200000
  • PLX 0000 0000 0PQ5 0000

What regular expression am I looking for? The language I use is C #

+5
source share
3 answers

Adding an answer to @Zenoo if you want to take %20 into account as a single occurrence, you can use the following expression:

 [pP]([a-zA-Z0-9 +-]|(%20)){14,}0000 

where you basically add %20 to the list of possible characters as one occurrence.

Here is a live demo that you can use to test all the desired examples (I already including the ones you provided), with all the necessary technical explanations that I copy below:

  • pP matches one character in the pP list (case sensitive)
  • 1st capture group ([a-zA-Z0-9 + -] | (% 20)) {14,} {14,} Quantifier - Matches between 13 and unlimited time, as many times as possible, returning as necessary ( greedy)
  • 1st alternative [a-zA-Z0-9 + -] Matches one character in the list below [a-zA-Z0-9 + -] az one character in the range between index 97 and z (index 122) (with case sensitive)
  • AZ - one character in the range between A (index 65) and Z (index 90) (case sensitive) 0-9 one character in the range from 0 (index 48) and 9 (index 57) (case sensitive)
  • + - matches one character in the + - list (case sensitive)
  • Second alternative (% 20) Second capture group (% 20)% 20 matches% 20 characters literally (case sensitive)
  • 0000 matches the letters 0000 literally (case sensitive)
+4
source

You can try this RegEx: [pP][a-zA-Z0-9- +%]{13,}0000

[pP] matches one character in the pP list (case sensitive)

{13,} corresponds to 13 or more iterations [a-zA-Z0-9- +%]

  • az one character between a (index 97) and z (index 122) (case sensitive)
  • az one character between a (index 65) and z (index 90) (case sensitive)
  • 0-9 one character between 0 (index 48) and 9 (index 57) (case sensitive)
  • - +% matches one character in the list - +% (case sensitive)

0000 matches the letters 0000 literally (case sensitive)

+3
source

A regular expression can be used to exactly match the format:

 ^(?=.{14,}0000$)[pP][a-zA-Z0-9]*(?:(?(1)\1|([- +]|%20)?)[a-zA-Z0-9]+)*$ 
  • (?=.{14,}0000$) asserts a string of length> = 18 and ends with the character 0000
  • (?(1)\1|([- +]|%20)?) Is an if condition that checks if the separator matches, it should be used as the next separator

Live demo

+2
source

Source: https://habr.com/ru/post/1275360/


All Articles