Reputation: 9
I currently have 2 Regular expressions that I am trying to combine but I don't know how since I'm a little new to all this: .*\Q%s\E.*
and ^(.*?),
So basically given a string I want to treat the quotes as literals and get all characters of the string up until a comma appears.
Upvotes: 0
Views: 49
Reputation: 1130
I dont think you need to use regex for this. There is a simpler way, using explode
.
You will get an array of each string broken up at the comma. If there are no commas, then you will get an array with only one value, the string. It is hard to input this example into your code, as there is none, so here it is:
$results = explode( ",", "testing this , string" )
Will return this:
array(2) {
[0]=>
string(13) "testing this "
[1]=>
string(7) " string"
}
Upvotes: 1
Reputation: 2304
If you can provide a little more information as far as some examples of strings that would be parsed, I can edit this answer a little further. But for example purposes if you had a string such as:
This is a "quoted string" ending in a comma,
A simple regular expression such as:
/([\s\w"']+),/
Based on your initial requirement of "strings with quotes treated as literals" would capture.
The regex would give you a match group of:
[0]: This is a "quoted string" ending in a comma,
[1]: This is a "quoted string" ending in a comma
With 1
, obviously being the indices you want to save because when using groups ()
, 0
will always be the full match.
Edit: Ice76's answer on using explode to split your string is also a great (and much simpler) alternative.
Upvotes: 0