This may be a lame question but I am a total novice with regular expressions. I have some text data in the format:
Company Name: Name of the company, place.
Company Address: Some,
address, here.
Link:
http://www.somelink.com
Now, I want to use a regex to split these into an array of name : value pairs. The regular expression I am trying is /(.*):(.*)/ with preg_match_all() and it does work well with the first two lines but on the third line it returns “Link: http:” in one part and “//www.somelink.com” in other.
So, is there any way to split the line only at the first occurrence of the character ‘:’?
You probably want something like
/(.*?):(.*)/. The?after the*will make it “non-greedy”, so it will consume as little text as possible that way. I think that will work for your situation. By default,*is “greedy”, and tries to match as many repetitions as it can.Edit: See here for more about matching repetition using the
*and+operators.