简体   繁体   中英

How do I match any character but “&&”?

I have the following string:

a simple string that may contain this & that but I want it to skip &&. Do you follow? 

I am working with the following regex to try to split it on a double occurrence of "&&":

[^&]*\s

This picks up the single "&" occurrence.

I've also tried:

[^&]{2}*\s

But that doesn't pick up anything.

The stuff I have found online apply to PCRE regex and I am unable to find an RE2 type solution that go uses.

https://regex101.com/r/kgVVvB/1

Normally, you would be able to use (?:(?!&&|(?<=&)&).)+ , but since golang doesn't currently support lookaheads or lookbehinds , you have to hack your way around this using regex. Obviously, using string functions may work for your case, but as you mention in the comments below your question this is part of a larger regex, so here it is:

See regex in use here

(?:(?:^|[^&])&(?:[^&]|$)|[^&])+
  • (?:(?:^|[^&])&(?:[^&]|$)|[^&])+ Match either of the following one or more times
    • (?:^|[^&])&(?:[^&]|$)
      • (?:^|[^&]) Assert position at the start of the line or match any character that is not &
      • & Match this literally
      • (?:^|[^&]) Assert position at the end of the line or match any character that is not &
    • [^&] Match any character that is not &

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