Home > Back-end >  Regex: Capture everything after first colon if colon exists otherwise capture everything
Regex: Capture everything after first colon if colon exists otherwise capture everything

Time:09-22

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.

CodePudding user response:

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.

  • Related