简体   繁体   中英

REGEX Python right quotation mark

At first this is my REGEX until now

(``|,,|")([^"|^'{2}|`{2}|^,{2}]*)(''|``|")

I think that negation in the secound group that I setted isnt working correctly. I seperated all my negations like you can see, I dont know if that can work.

This should for example find this sentence

``Some question is here ,some text here''

but not this one

``Some question is here ,,some text here''

Im already trying to find the solution for hours but im new at Regex and I couldnt find the solution. Thanks in advance for any help.

You can use

^(``|,,|")((?:(?!"|'{2}|`{2}|,{2}).)*)(''|``|")$

See the regex demo

Details :

  • ^ - start of string/line
  • (``|,,|") - two backticjks, commas or a double quotation mark
  • ((?:(?,"|'{2}|`{2}|.{2}).)*) - any single char other than line break chars, as many as possible, that is not a " and does not start a '' , two backticks, or ,, char sequence
  • (''|``|") - '' , two backticks, or a "
  • $ - end of string/line.

See a Python demo :

import re
rx = r"""^(``|,,|")((?:(?!"|'{2}|`{2}|,{2}).)*)(''|``|")$"""
texts = ["``Some question is here ,some text here''", "``Some question is here ,,some text here''"]
for text in texts:
    m = re.search(rx, text)
    if m:
        print(f"MATCHED: {text}")

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