Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for 1 or 2 digits, optional non-alphanumeric, 2 known alphas

Tags:

c#

regex

grep

I've been bashing my head against the wall trying to do what should be a simple regex - I need to match, eg 12po where the 12 part could be one or two digits, then an optional non-alphanumeric like a :.-,_ etc, then the string po.

The eventual use is going to be in C# but I'd like it to work in regular grep on the command line as well. I haven't got access to C#, which doesn't help.

like image 352
Ben Avatar asked Mar 01 '11 13:03

Ben


People also ask

Which regex matches one or more digits?

Occurrence Indicators (or Repetition Operators): +: one or more ( 1+ ), e.g., [0-9]+ matches one or more digits such as '123' , '000' . *: zero or more ( 0+ ), e.g., [0-9]* matches zero or more digits. It accepts all those in [0-9]+ plus the empty string.

What does \+ mean in regex?

Example: The regex "aa\n" tries to match two consecutive "a"s at the end of a line, inclusive the newline character itself. Example: "a\+" matches "a+" and not a series of one or "a"s. ^ the caret is the anchor for the start of the string, or the negation symbol.

What does \d mean in regex?

The \D metacharacter matches non-digit characters.


1 Answers

^[0-9]{1,2}[:.,-]?po$ 

Add any other allowable non-alphanumeric characters to the middle brackets to allow them to be parsed as well.

like image 132
eykanal Avatar answered Sep 30 '22 08:09

eykanal