searching for one or two numbers fails??

federente

Newcomer
Joined
Sep 13, 2004
Messages
2
hi,

I've a problem with a regular expression..
I'll use it, to..
match a bank account line, so accept the complete line
and filter two 6-digit-numbers out of the string to put it in reference 1 and 2
(or only one, thats the problem)
(for later use with coldfusions REreplace function ..
:select all text and replace it with the numbers, found in the text, separated by kommas )

heres the regEx:
^(?:.*?)(\d{6,6})(?:-\d{2,2}|)(?:.*?)(?:(\d{6,6})(?:-\d{2,2}|))?

now, you can see the problem in the result of example 2 (using regexTester).
where is the second number,
the regex doesnt find it, but why does it succed in searching in example 3?

thank you

example rows:
1: hans meier rechnung nr.04-123456-04 fa any
2: hans meier rechnung nr.04-123456 777777-04 fa any
3: hans meier rechnung nr.04-123456777777-04 fa any

results:
1: ok, data in ref. 1
2: not ok (data in ref.1 ok, but 2 is empty)
3: ok data in ref. 1+2 (removed whitespace between the numbers)
 
Clarification

federente said:
I'll use it, to..
match a bank account line, so accept the complete line
and filter two 6-digit-numbers out of the string to put it in reference 1 and 2

example rows:
1: hans meier rechnung nr.04-123456-04 fa any
2: hans meier rechnung nr.04-123456 777777-04 fa any
3: hans meier rechnung nr.04-123456777777-04 fa any

1) You would like to find only rows with two 6-digit numbers?
2) And then pull out those two 6-digit numbers for use later?
3) So only row 2 above is a good row, and from that row you want the numbers 123456 and 777777 for use later?
 
Answer

Richard Crist said:
1) You would like to find only rows with two 6-digit numbers?
2) And then pull out those two 6-digit numbers for use later?
3) So only row 2 above is a good row, and from that row you want the numbers 123456 and 777777 for use later?

Since the poster hasn't replied and this thread seems popular, I shall post the answer.

\D*(\d{6})\D+(\d{6})\D*

This says find:
zero or more non-digit characters
followed by a 6 digit number
followed by 1 or more non-digit characters
followed by zero or more non-digit characters

The numbers you want will be in groups 1 and 2, represented by the first and second parentheses.

Information can be found here.
 
Correction / Additional Information

Richard Crist said:
\D*(\d{6})\D+(\d{6})\D*

This says find:
zero or more non-digit characters
followed by a 6 digit number
followed by 1 or more non-digit characters
followed by zero or more non-digit characters

The last reply I posted was correct except for my word translation of the regular expression. It should read as follows (one line added before the end):

zero or more non-digit characters
followed by a 6 digit number
followed by 1 or more non-digit characters
followed by a 6 digit number
followed by zero or more non-digit characters

Hope I didn't confuse anyone too much the first time. :p

Also if you wanted to search for lines with just one 6 digit number you could use:

\D*(\d{6})\D*

And if you wanted to search for lines with either one OR two 6 digit numbers you could "OR" them together using the vertical bar (pipe) character "|" as follows:

\D*(\d{6})\D*|\D*(\d{6})\D+(\d{6})\D*

Regular expression bonanza can be found here. :)
 
Back
Top