Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for specific regex

Tags:

c#

regex

I've been looking to create a regex for my specific situation. The furthest i've come with my own limited knowledge of Regex and by searching on StackOverflow is this Regex:

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

I'm looking for a Regex which forces the string to:

  • start with the letter 'p' or 'P', so lower and uppercase
  • is not shorter than 19 characters long
  • may contain '-', '%20', ' ' or '+'
  • end with '0000'
  • be alphanumeric inbetween the 'p' and the four zero's and the '-', '%20', ' ' or '+' delimiters

Examples of strings that should match the Regex:

  • 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 is the Regex I'm looking for? The language I'm using is C#

like image 259
Jeroen Avatar asked Feb 14 '18 09:02

Jeroen


People also ask

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

How do you search for a regex pattern at the beginning of a string?

The meta character “^” matches the beginning of a particular string i.e. it matches the first character of the string. For example, The expression “^\d” matches the string/line starting with a digit. The expression “^[a-z]” matches the string/line starting with a lower case alphabet.

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1. 1* means any number of ones.


2 Answers

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

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

where you basically add to the possible character list %20 as a single occurrence.

Here is the live demo, which you can use to test all the examples you want (I already included those you provided), with all the technical explanations needed, which I am copying below:

  • pP matches a single character in the list pP (case sensitive)
  • 1st Capturing Group ([a-zA-Z0-9 +-]|(%20)){14,} {14,} Quantifier — Matches between 13 and unlimited times, as many times as possible, giving back as needed (greedy)
  • 1st Alternative [a-zA-Z0-9 +-] Match a single character present in the list below [a-zA-Z0-9 +-] a-z a single character in the range between a (index 97) and z (index 122) (case sensitive)
  • A-Z a single character in the range between A (index 65) and Z (index 90) (case sensitive) 0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
  • +- matches a single character in the list +- (case sensitive)
  • 2nd Alternative (%20) 2nd Capturing Group (%20) %20 matches the characters %20 literally (case sensitive)
  • 0000 matches the characters 0000 literally (case sensitive)
like image 77
Francesco B. Avatar answered Oct 16 '22 14:10

Francesco B.


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

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

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

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

0000 matches the characters 0000 literally (case sensitive)

like image 38
Zenoo Avatar answered Oct 16 '22 13:10

Zenoo