hiFI
hiFI

Reputation: 1961

Regular Expression to Capture for Number within Quotes and with/out Space

I have a regular expression that will capture any string literal that starts with JKB and the number following it.

I have the following Reg Ex built for it:

(?'Account'JKB[0-9]+LC),(?'Amount'[0-9,.]*)

It captures the Amount in all except the first and second from the following list:

  1. 29/7/2021,FDC Bank PLC / Mr P.S.R STEVE SMITH,JKB99999LC," 9,500,000 "
  2. 29/7/2021,FDC Bank PLC / Mr P.S.R STEVE SMITH,JKB99999LC,"9,500,000"
  3. 29/7/2021,FDC Bank PLC / Mr P.S.R STEVE SMITH,JKB99999LC,9500000
  4. 29/7/2021,FDC Bank PLC / Mr P.S.R STEVE SMITH,JKB99999LC,9,500,000

How can I make my RegEx accommodate to capture 1st and 2nd item too; at least the number

Here is my working: https://regex101.com/r/mcQtA9/2

Upvotes: 1

Views: 45

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626870

You need to assure the substring between the two group patterns is consumed. You can use

(?<Account>JKB[0-9]+LC),"?\s*(?<Amount>[0-9,.]*)
(?<Account>JKB[0-9]+LC),\W*(?<Amount>[0-9,.]*)

See the regex101 demo and the .NET regex demo:

enter image description here

I used "?\s* in the first pattern to match an optional " and then zero or more whitespace chars. In the second variation, I just used \W* to match any zero or more non-word chars.

I prefer (?<name>...) named capturing group syntax, but you may use single quoted variant, if you are more accustomed to that syntax.

Upvotes: 1

Related Questions