BreakPoint

cancel
Showing results for 
Search instead for 
Did you mean: 

Regular Expressions Board

We have an excel board. Should we have a repository for regular expressions too. This might be just and useful as the Excel board.

Tim
GHSP
Message 1 of 150
(19,099 Views)

I'll begin with this one that's more of a curiosity than a useful snippet.

 

19455iD57361499268F06A

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.

 

  • The pipe causes the regex to match either the expression on the left OR the one on the right
  • the carrot looks for a match at the beginning of the input string
  • the dollar sign looks for a match at the end of the input string
  • the question mark makes the preceding item optional (zero or one of whatever is before it)
  • beginning a regex with a carrot and ending it with a dollar sign only returns an exact match (in this case, either zero or one "1"s)
  • and then, the right side of the pipe...
Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

Message 2 of 150
(19,060 Views)

Why doesn't Johnny use Regular Expressions?

Spoiler

His mother doesn't let him play with matches. 😄

 

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

Message 3 of 150
(19,022 Views)

Took me a while to find the carrott.

 

Spoiler

I think you mean "caret" (pronounced same as carrott).....

 

Shane

Message 4 of 150
(18,996 Views)

EDIT: retracted.

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

Message 5 of 150
(18,978 Views)

Not to be confused with irregular expressions. 😄

Message 6 of 150
(18,961 Views)

@altenbach wrote:

Not to be confused with irregular expressions. 😄


You have to be an irregular guy to be able to understand irregular expressions.

- tbob

Inventor of the WORM Global
Message 7 of 150
(18,952 Views)

19241iF4BA6DB8BE29CDD9

 

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:

 

19245i6699BF2C7896A1A6


Please don't ever do this again. 😛

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

Message 8 of 150
(18,882 Views)

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.

19243iC7BBA3E0B2773182

 

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.

 

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

Message 9 of 150
(18,823 Views)

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.

 

Ton

Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
Nederlandse LabVIEW user groep www.lvug.nl
My LabVIEW Ideas

LabVIEW, programming like it should be!
Message 10 of 150
(18,775 Views)