1252748
1252748

Reputation: 15379

match text strings not contained in quotes

(?:'.*?') 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

Answers (4)

user557597
user557597

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

Peter Alfvin
Peter Alfvin

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

anubhava
anubhava

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

ArtK
ArtK

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

Related Questions