简体   繁体   中英

Regular expression for single quotes

After a long time of trying to find a good regular expression to find, and then replace, single quotes ('') with single guillemets (›‹), taking care of apostrophes, I came up with this:

'((([^']*')*[^']*)+)'

I've successfully tested it at PHP Live Regex. However, it only works with preg_match and only sometimes with preg_replace (as replacement, I use ›$1‹ ).

Could it be a problem with backreferences? Does anyone know a better solution?

EDIT :

The issue I get is catastrophic backtracking when I am trying to change strings similar to

one ‘two’s’ ‘three’ four

to

one ›two’s‹ ›three‹ four

You can use

‘((?:[^‘]*’)*[^‘]*)’

See demo

The main change is removing outer (...)+ , and that created nested quanitifier issue. Both [^']*')* and [^']* can match emtpy strings, and when enclosed into group with + quantifier, the number of steps to complete the match grew exponentially.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM