Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex - Matching text AFTER certain characters

Tags:

regex

ruby

I want to scrape data from some text and dump it into an array. Consider the following text as example data:

| Example Data
| Title: This is a sample title
| Content: This is sample content
| Date: 12/21/2012

I am currently using the following regex to scrape the data that is specified after the 'colon' character:

/((?=:).+)/

Unfortunately this regex also grabs the colon and the space after the colon. How do I only grab the data?

Also, I'm not sure if I'm doing this right.. but it appears as though the outside parens causes a match to return an array. Is this the function of the parens?

EDIT: I'm using Rubular to test out my regex expressions

like image 767
Tushar Garg Avatar asked Dec 17 '12 23:12

Tushar Garg


2 Answers

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.

like image 66
robSE13 Avatar answered Sep 30 '22 17:09

robSE13


You could change it to:

/: (.+)/

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

/(?<=: ).+/
like image 30
Ry- Avatar answered Sep 30 '22 16:09

Ry-