How to match something with regex that is not between two special characters?

后端 未结 3 1269
梦谈多话
梦谈多话 2020-12-03 11:14

I have a string like this:

a b c a b \" a b \" b a \" a \"

How do I match every a that is not part of a string deli

3条回答
  •  失恋的感觉
    2020-12-03 12:08

    js-coder, resurrecting this ancient question because it had a simple solution that wasn't mentioned. (Found your question while doing some research for a regex bounty quest.)

    As you can see the regex is really tiny compared with the regex in the accepted answer: ("[^"]*")|a

    subject = 'a b c a b " a b " b a " a "'
    regex = /("[^"]*")|a/
    replaced = subject.gsub(regex) {|m|$1}
    puts replaced
    

    See this live demo

    Reference

    How to match pattern except in situations s1, s2, s3

    How to match a pattern unless...

提交回复
热议问题