Regular Expression: Search for string with alternative last character

I use KM to run a macro on invoices so I can rename them to something that's easy to find later.

Such invoices contain job references in the form of "WO-\d\d\d\d" most of the time (see 1st example below) but sometimes they have a letter after the last number (2nd example below).

How I can do a RegEx search that would find the longer string of the following two (when there is one)?:

Examples:

 WO-0237
 WO-0237a

IOW, I would need the KM RegEx search formula to give priority the WO number string that has the letter at the end.

I know that using "WO-\d\d\d\d" will find "WO-0237" in both cases but when there's a letter appended (like the 2nd example above) I need it to get that one instead.

I'm wondering if there's a way to do this with a Regular Expression or If I would instead need to do an IF/THEN/ELSE kind of thing.

Thanks

Depending on the context of the codes (we would need to see an example of a whole line), you may be able use \b which matches word boundaries.

or perhaps something like WO-[0-9]{4}[a-z]{0,1}

Hi ComplexPoint,

Here's an example of the invoice reference line that does not have the letter after the 4 digits:

 WO-0243 Mary Had a Little Lamb.xlsx

And here's an example where it does:

 WO-0243a God Bless America.xlsx

Also, I just tried your code and it successfully found both. There will never be both in the same invoice so I think this is all I need : )

But just for further education on the subject. How can you use RegEX if it was a situation where both examples above would indeed exist on the same document and we had to only pick the one with the letter after the 4 digits? Just curious.

Thanks

Hey @project_guru,

That's good, because you cannot prioritize one match above another – either you match or you don't match.

When necessary It's easy enough to weed stuff out with more than one pass.

WO-[0-9]{4}[a-z]?

The ? means 1 or none in this context.

-Chris

Thank you very much!

1 Like