higuys
higuys

Reputation: 17

What Regex would capture everything from second : mark to the end of a line?

Target:

: (x.S.) xxxx xxxx (xxx  xx xx xxxx): Expected xxxxxxx 2021

My regex:

\:.*\r 

Adding ^\r or changing \r to \n don't work.

Upvotes: 2

Views: 70

Answers (1)

JvdV
JvdV

Reputation: 75840

If you can say that your strings always contain two colon's I think I'd go with something along the lines of:

\b[^:\r\n]+$
  • b - Word boundary
  • [^:\r\n]+) - Negated colon, carriage return or newline (one or more)
  • $ - End string ancor

Online Demo


If you want to explicitly test for two colon's in your string and return everything after the second one (including possible colons), you might want to use:

^(?:[^:]*:){2}\s*(.*)$
  • ^ - Start string ancor
  • (?:- Non-capturing group
    • [^:]*): - Negated colon zero or more times followed by colon
    • {2} - Repeat non-capturing group two times
  • \s* - Zero or more spaces
  • (.*) - Capturing group holding anything but newlines
  • $ - End string ancor

Online Demo


Though, as most languages do also have some sort of Split() function, you could decide on researching that and ditch regular expressions.

Upvotes: 2

Related Questions