Walucas
Walucas

Reputation: 2568

Regex capture string up to character or end of line

I need one regex to capture a string up to a :, but the problem is that the : is not always there.

At this moment I am able to capture the groups when I have the : but not when I dont.

Not sure what I am doing wrong.

strings to capture

XXX 1 A:B (working)

XXX 1 A: (working)

XXX A (not working)

My regex: ^(?P<grp1>[A-Z]{3,10})\s(?P<grp2>.*)(?=\:)(?:.)*$

Upvotes: 1

Views: 1938

Answers (2)

The fourth bird
The fourth bird

Reputation: 163352

Optionally match a single : after it

^(?P<grp1>[A-Z]{3,10})\s(?P<grp2>[^:\r\n]*)(?::[^:\r\n]*)?$
  • ^ Start of string
  • (?P<grp1>[A-Z]{3,10}) Group grp1
  • \s Match a whitspace char
  • (?P<grp2>[^:\r\n]*) Group 2 grp2 Match any char except : or a newline
  • (?::[^:\r\n]*)? Optionally match a single : between optional chars other than : or a newline
  • $ End of string

Regex demo

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

You can use

^(?P<grp1>[A-Z]{3,10})\s(?P<grp2>.*?)(?::.*)?$

See the regex demo. Details:

  • ^ - start of string
  • (?P<grp1>[A-Z]{3,10}) - Group "grp1": three to ten uppercase letters
  • \s - a whitespace
  • (?P<grp2>.*?) - Group "grp2": any zero or more chars other than line break chars, as few as possible
  • (?::.*)? - an optional group matching any zero or more chars other than line break chars as many as possible
  • $- end of string.

Upvotes: 1

Related Questions