We have an excel board. Should we have a repository for regular expressions too. This might be just and useful as the Excel board.
I'll begin with this one that's more of a curiosity than a useful snippet.
I saw it here (with an explanation) and had to test it in LabVIEW. Someone is very clever.
What you can get out of this is an exercise in familiarity with the elements of Regular Expressions.
Why doesn't Johnny use Regular Expressions?
His mother doesn't let him play with matches. 😄
This example demonstrates the oft-used expression: ".*" (dot star). You remember (right?) that a dot matches "any single character except line break characters \r and \n" and that the star "[r]epeats the previous item zero or more times." The parentheses save whatever is matched by the expression between them (you can use this later in your expression as a "backreference"). In this example I'm looking for the word "and", but I don't want to have to strip off the "start", "stop" or the whitespace later. I only use the Whole Match output to illustrate the function; the string I'm interested in is returned in the first submatch. (Note: MRE is an expandable node. You need to drag the bottom handle down to access the submatch terminals.)
This method should replace the following snippet:
Please don't ever do this again. 😛
Submatches are useful in extracting a part of your match but they also can also be reused in the regexp. An example is to match beginning and ending tags when you don't know what tag you're looking for.
The brackets are not special characters so they'll result in a literal match. The expression "(\w*)" creates a submatch that will contain zero or more word characters. The second element should look familiar to you but the last element is special. The "</" marks the beginning of a closing tag, but you can't just look for the first instance of this because there may be nested tags. Use the backslash to insert the first submatch (called a backreference) indicated by the number one. The desired text is in submatch 2, the second set of parentheses. You can test this by replacing "sarcasm" by another string.
It might be an idea to add a 'RegEx' group on the communities?
In this group we could host a page with a table showing 'input text', 'Regex', 'match' and a description of the actual working of the regex.
That way it's better to manage and search than a thread with numerous responses.