Developing generic regex sequences
-
Hi @peterjones, @alan-kilborn and All,
I gave a similar answer to @vijay-s ( refer here ), but Peter BRILLIANTLY beat me at it and gives us a complete panel of the look-aheads to use in order to simulate the main logical combinations !
Now, Peter, I think it would be worth, in the general case, to add a
^
anchor, right in front of all these formulas !Best Regards,
guy038
-
@guy038 said in Developing generic regex sequences:
Now, Peter, I think it would be worth, in the general case, to add a ^ anchor, right in front of all these formulas !
The logic itself is independent of what you anchor it in. My two usage examples in my first post about “Logic Gates for Regular Expressions” show that you can use these standalone generic anchored either per-line or per-file depending on what you wrap around them. With generic, you could even stick these after some other match on the line, saying “after some prefix, match aaa or bbb” or similar. Hence, I didn’t want to specify the anchors in my generic expressions.
-
@PeterJones said in Developing generic regex sequences:
Updating with n-term rather than just two-term:
Nice use of a table in a posting here, as well. :-)
Seriously, valuable information here. Kudos. -
So as I often do, I dug in a bit deeper to what Peter presented.
My conclusion is that pointing novices at regular expressions here and expecting them to solve their own related problems may not be super-successful.
It isn’t that all the needed info isn’t here – it is – it just may require some base knowledge to be applicable, without readers saying “Huh?”.So maybe some really concrete examples help. In that light, my contribution will be how to match entire lines meeting the logic criteria that Peter brought to the table.
Say you want to match some particular combination of
Bob
andTed
on a line – here’s information on doing that:Logic Expression to use Match entire line when… OR (?-s)(?:(?=.*Bob|.*Ted))^.*(?:\R|\z)
Bob
orTed
(or both) is present, in either orderAND (?-s)(?:(?=.*Bob)(?=.*Ted))^.*(?:\R|\z)
both Bob
andTed
are present, in either orderXOR (?-s)(?:(?=.*Bob)(?!.*Ted)|(?!.*Bob)(?=.*Ted))^.*(?:\R|\z)
Bob
orTed
is present, but not when both are presentNOR-1 (?-s)(?:(?!.*Bob)(?!.*Ted))^.*(?:\R|\z)
neither Bob
/Ted
are present (form 1)NOR-2 (?-s)(?:(?!.*(Bob|Ted)))^.*(?:\R|\z)
neither Bob
/Ted
are present (form 2)NAND (?-s)(?:(?!(?=.*Bob)(?=.*Ted)))^.*(?:\R|\z)
neither are present or one is present, but not when both are present I took a little liberty with Peter’s original “notes” table column; changed it up a bit. Also, obviously I only did a “two term” example.
Maybe I’m off-base and this doesn’t provide additional insight on exactly how to use Peter’s info, but hopefully it does.
-
@Alan-Kilborn said in Developing generic regex sequences:
My conclusion is that pointing novices at regular expressions here and expecting them to solve their own related problems may not be super-successful.
That’s why I posted here, rather than separately. This thread is for “developing” the generic expressions, with lots of back and forth. The “final version” will be published to its own separate thread. (I probably shouldn’t’ve posted a link back to here from the inspiration thread, because this one wasn’t ready yet)
I think your table is a good practical example of how to use it.
-
So a note on “practicality” here…
Recently I had cause to implement some “OR” searches as described above.
I pulled up this thread for the “formula”, put my specific use-case data in, and pressed Find All in Current Document, and, well, …, waited, a loooong time for results to come back.
It turns out that the regexes specified above are fine for “small” data, but are rather inefficient for “bigger” data, or at least the size/type of data I had.Here’s an example:
The original “match entire line OR regex” above is(?-s)(?:(?=.*Bob|.*Ted))^.*(?:\R|\z)
For my data, that one took between one and two minutes to run.
If I change the regex to(?-s)^(?=.*?(?:Bob|Ted)).+
, that one runs so quickly that it is hard to time, except to say maybe it takes a second or so.Probably all of the regexes I presented in my table above could be better optimized. :-(
-
-
-
-
A year later, I finally got around to making the “table of contents” post in the FAQ: “FAQ Desk: Generic Regular Expresion (regex) Formulas”
For now, it’s linking to the in-thread versions of these generic expressions… but I highly encourage the developers of the expression to spin off a new blog post for each generic regex.
-
-
-
-
-
-
-
-
-
-
Hi all,
I’m wondering if this needs to be added to the Regex FAQ or another thread. @PeterJones @guy038There are some good regex tester sites out there. Here’s one and a search for others. It really helps in debugging regular expressions. Generally you add in test data and the regex, and the site will highlight the strings it matches.
Build and test regular expressions regex. Make a free account here to save your regexes. https://regex101.com/
Search for more: https://search.brave.com/search?q=free+account+test+regular+expression&source=webI hope this is helpful!
-
@c-bacca Ok this search is case sensitive.
Also, here’s an example with example data. https://regex101.com/r/Hfly86/1
-
@c-bacca said in Developing generic regex sequences:
I’m wondering if this needs to be added to the Regex FAQ or another thread.
Why would it need to be added where it already exists?
Or were you not aware that we have two different regex FAQ entries?
The second was the one discussed in this original topic: a table of contents of “generic” regex at https://community.notepad-plus-plus.org/topic/22673/faq-desk-generic-regular-expression-regex-formulas
But the much earlier regex FAQ explains where to get regex help, including links to the Notepad++ regex documentation, plus links to a lot more “regex tester” sites than you mentioned: https://community.notepad-plus-plus.org/topic/15765/faq-desk-where-to-find-regular-expressions-regex-documentation
-
@c-bacca said in Developing generic regex sequences:
There are some good regex tester sites out there. Here’s one and a search for others. It really helps in debugging regular expressions. Generally you add in test data and the regex, and the site will highlight the strings it matches.
Build and test regular expressions regex. Make a free account here to save your regexes. https://regex101.com/I suppose, but I don’t believe it uses the same regular expression engine as Notepad++, so it is of limited usefulness if you are going to use Notepad++ to do your regular expression searches and replacements.
-
@alan-kilborn ,
It actually let’s you use different regex flavors, (PCRE pre 7.3 and 7.3 plus, ECMAScript, Python, Golang, Java8, and .NET(C#), but the benefit of regex101.com, is that you can interactively see how your regex construction is going to work on the text you supply, either just Search or both Search and Replace. It’s actually educational to see what happens as you experiment with different regex constructions. You have to construct and then execute NPP to see results, and you’re not sure what you did wrong unless there’s a mode in NPP that does the same interactive result. I use it all the time to construct the regex I’ve been building, to be able to quickly see how the regex I’m constructing works on their supplied text. It’s just another tool, and it is recommended as Peter points out in the FAQ, which is where I learned about it. So I can see why someone might want to use that tool, Alan. :) -
-
This post is deleted!