0

I want to divide this kind of string into 3 groups

"02.01. 02.01. Pay card PN:123                                        6562,58 S"

group1 should match "02.01. 02.01."

group2 should match "Pay card PN:123 "

group3 should match "6562,58 S" (this part can vary by the amount i.e. 1,99 H )

my regex looks like this

^([0-9][0-9]\.[0-9][0-9]\. [0-9][0-9]\.[0-9][0-9]\.)(.*)(\d+\,[0-9][0-9].*)

but group3 will only match "2,58 S" instead of "6562,58 S"

What is the correct regex ?

tomfox66
  • 329
  • 3
  • 10
  • 17

1 Answers1

1

.* is greedy, so it will match as much as possible. Either make it non-greedy or don't use . but \s for example.

I would also make the Regex as specific as possible, i.e. include the known strings. Since you seem to know about \d, I would use that over [0-9] for readability reasons.

^(\d\d\.\d\d\. \d\d\.\d\d\.)\s+(Pay card PN:\d+)\s+(\d+,\d\d [HS])$

That way you'll notice when your input changes and you potentially need to rework the Regex

^                             beginning of line
(\d\d\.\d\d\. \d\d\.\d\d\.)   dates
\s+                           whitespace as separator
(Pay card PN:\d+)             card number
\s+                           white space as separator
(\d+,\d\d [HS])               amount
$                             end of line

Check it in RegexStorm.NET

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222