Regex - Matching text AFTER certain characters

In addition to @minitech's answer, you can also make a 3rd variation:

/(?<=: ?)(.+)/

The difference here being, you create/grab the group using a look-behind.

If you still prefer the look-ahead rather than look-behind concept. . .

/(?=: ?(.+))/

This will place a grouping around your existing regex where it will catch it within a group.

And yes, the outside parenthesis in your code will make a match. Compare that to the latter example I gave where the entire look-ahead is 'grouped' rather than needlessly using a /( ... )/ without the /(?= ... )/, since the first result in most regular expression engines return the entire matched string.


I know you are asking for regex but I just saw the regex solution and found that it is rather hard to read for those unfamiliar with regex.

I'm also using Ruby and I decided to do it with:

line_as_string.split(": ")[-1]

This does what you require and IMHO it's far more readable. For a very long string it might be inefficient. But not for this purpose.


You could change it to:

/: (.+)/

and grab the contents of group 1. A lookbehind works too, though, and does just what you're asking:

/(?<=: ).+/

Tags:

Ruby

Regex