Take the 2-minute tour ×

This is a follow-up to a previous question. I have a string "Test 999-99-9", how would I match on everything except the last -9 part? Keep in mind, the last -9 may or may not be there, but if it is, I want to ignore it and match on the rest of the string. Any suggestions?

Alternatively, if it ignored the entire 999-99-9 or 999-99 part, and just returned the "Test" part, that would be fine, too. It seems like that may be easier to do. I basically want to take the following expression and invert it to return the other half of the string: (\d{3}-\d{2}|\d{3}-\d{2}-\d{1})$

share|improve this question
Which language? –  kapa Jul 6 '12 at 13:43
please post some possible inputs and the respective desired outputs –  heltonbiker Jul 6 '12 at 13:44
Well, like I explained above, if the input is either "Test 999-99" or "Test 999-99-9", I just want it to output "Test 999-99" (ignore the last -9 if it is present). Or, as I explained in the 2nd paragraph, if it just returned "Test", that would be fine too (I can isolate the 999-99 pattern using a different expression I already wrote). Keep in mind, the character part of the string could be more than one word, if that makes a difference. –  tjc59 Jul 6 '12 at 13:49
add comment

2 Answers

up vote 1 down vote accepted

RegEx to ignore 999-99-9 and just return "Test" part:

^([\w ]+) [\d]{3}-[\d]{2}-?[\d]?$

OCR Software supports groups:


Note: The parentheses determine which information is extracted from the text. The other characters determine the pattern that will be looked for. For example, \d\d\d-\d\d-(\d\d\d\d) will find the social security number and return the last four digits of it.

share|improve this answer
I am testing this at regexpal.com and it seems to return the entire string, not just the first part. –  tjc59 Jul 6 '12 at 13:58
It matches the whole string, but returns just the "Test" part as group 1, which you can access using $1 with most regex systems –  Alex W Jul 6 '12 at 14:03
I guess I'm not familiar with groups, or how to access them. Do I just tack the $1 onto the end of the expression? Also, the "Test" part may be multiple words; I'm not sure if this will match if multiple words. Sorry for the confusion. –  tjc59 Jul 6 '12 at 14:10
This is for Laserfiche Quick Fields (laserfiche.com/en-us/products/quick-fields) zone OCR scanning software. You can inject regex when you map the OCR zone to the metadata field, so the regex basically has to be stand-alone. And I'm not sure what language it would be. Basically when it reads a string, I want it to split it based on this pattern and store the strings in different metadata fields. –  tjc59 Jul 6 '12 at 14:13
@tjc59 Ok, updated my answer to match multiple words. It depends on which language you are using as to how to display match groups. –  Alex W Jul 6 '12 at 14:13
show 7 more comments

^(Test \d{3}-\d{2})(-\d{1})?$ will return everything except the last "-9" from your example, whether the "-9" is present or not.

share|improve this answer
This seems to return everything, no matter what, when I test it. Also, the word "Test" is not static; it could be a whole series of different words. I'm basically trying to do an inverse match on everything that doesn't match the 999-99 or 999-99-9 pattern. –  tjc59 Jul 6 '12 at 13:54
This is horrible RegEx. You are literally matching the word "Test" and you are matching the last 9, but treating it as a second group. –  Alex W Jul 6 '12 at 13:56
add comment

Your Answer


