ryanq.feeney
ryanq.feeney

Reputation: 31

Regex: Capture everything after first colon if colon exists otherwise capture everything

Using regex alone, I am having trouble capturing things after a field entry that comes in one of three ways:

Address: 123 Test Lane, City St
Address:123 Test Lane, City St
123 Test Lane, City St

I need to extract only the address, name, other info.

I found Regex to capture everything after optional token

and made a simpler regex ^(?:.*:\s?)?\K.+ that works but the system I'm using does not support the \K operator. I'm hoping I'm not out of options here.

Upvotes: 1

Views: 1049

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626803

You can use

[^:\s][^:\n]*$

Details:

  • [^:\s] - any char other than : and whitespace
  • [^:\n]* - zero or more chars other than : and LF
  • $ - end of string / line (if multiline mode is turned on).

See the regex demo.

Upvotes: 2

Related Questions