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.
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.
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.
The \D metacharacter matches non-digit characters.
^[0-9]{1,2}[:.,-]?po$
Add any other allowable non-alphanumeric characters to the middle brackets to allow them to be parsed as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With