Reputation: 15379
(?:'.*?')
matches text inside single quotes. I want to match string of text that are not inside quotes though. [^(?:'.*?')]
is not working for me though: it matches every character that is not a single quote. How can I change it to match text not within quotes?
Upvotes: 1
Views: 77
Reputation:
I think a general purpose way is a loop matching and catenating each group 1 value
Don't need the \G
anchor because the characters are either quote or not-quote.
# (?:(?:'[^']*')+|((?:[^']+|'(?![^']*'))+))
(?:
(?:
' [^']* '
)+
|
( # (1 start)
(?:
[^']+ # not '
|
' # or, ' not followed by another '
(?! [^']* ' )
)+
) # (1 end)
)
Upvotes: 0
Reputation: 29439
I've never learned the php function/method calls, but the following regex will "capture" the text outside of quotes:
(?<=^|\G)(?:'[^']*')?([^']+)
as shown in this rubular
Upvotes: 0
Reputation: 786091
You can use preg_replace
to replace all quoted text with empty string to get all the text that is outside quote (assuming quotes are balanced and unescaped):
$s = "abc '123' foo";
$r = preg_replace("/'[^']*'/", "", $s);
//=> "abc foo"
Upvotes: 1
Reputation: 1179
I don't know the purpose of your actions but you could convert the string to Char array and match Unicode codes.
Upvotes: 0