Found the solution. For future reference, Python Scrip wants to see the single line flag used in the search. editor.rereplace(r"(^F1 VA \d+ )(?=[\s\S]*\1)",r"! \1")
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Right at the front, just like I said. I tried it with your data set and got the results you expected.
editor.rereplace(r"(?s)(^F1 VA \d+ )(?=.*\1)", r"! \1")
without the (?s) the . won't match end-of-line characters. With it, it will. That way you can still use the . and don't have to resort to the [\s\S] trick (although that works fine, too).
Aside: When you use the .* pattern, you should always first consider using it as .*? so that it matches minimally.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Ah! I see what you meant now. I started learning Python and regex thanks to this add in. I like how elegent and streamlined that addition was, and it worked perfectly. Thanks for the input!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Does Python Script support lookahead? I’m trying to get this regex to work, but it keeps failing.
For more backstory (and an example dataset), here’s a link to a reddit post.
https://www.reddit.com/r/regex/comments/eutmta/finding_duplicates_using_a_lookahead/
Thanks in advance for any input! This is driving me crazy.
Found the solution. For future reference, Python Scrip wants to see the single line flag used in the search.
editor.rereplace(r"(^F1 VA \d+ )(?=[\s\S]*\1)",r"! \1")
"single line flag"?
"Pythonscript wants..." -> it's about what YOU want :-)
I think I see what you are saying though.
You could have left your original regex alone and just added
(?s)
to the front of it.Last edit: Sasumner 2020-01-30
I tried using your suggestion in a number of different ways, but didn't have any luck. Where in the line were you expecting it to go?
Right at the front, just like I said. I tried it with your data set and got the results you expected.
editor.rereplace(r"(?s)(^F1 VA \d+ )(?=.*\1)", r"! \1")
without the
(?s)
the.
won't match end-of-line characters. With it, it will. That way you can still use the.
and don't have to resort to the[\s\S]
trick (although that works fine, too).Aside: When you use the
.*
pattern, you should always first consider using it as.*?
so that it matches minimally.Ah! I see what you meant now. I started learning Python and regex thanks to this add in. I like how elegent and streamlined that addition was, and it worked perfectly. Thanks for the input!