# BreakPoint

cancel
Showing results 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 149
(12,272 Views)

## A Regular Expressions to check for prime numbers

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.

• 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 149
(12,233 Views)

## Regular Expressions humor

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 149
(12,195 Views)

## Re: A Regular Expressions to check for prime numbers

Took me a while to find the carrott.

Spoiler

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

Shane

Message 4 of 149
(12,169 Views)

## Re: A Regular Expressions to check for prime numbers

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 149
(12,151 Views)

## not to be confused ...

Not to be confused with irregular expressions. 😄

LabVIEW Champion. It all comes together in GCentral
What does "Engineering Redefined" mean??
Message 6 of 149
(12,134 Views)

## Re: not to be confused ...

@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 149
(12,125 Views)

## an example of how Match Regular Expression beats Match Pattern

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. 😛

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 149
(12,055 Views)

## one way to use a backreference

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.

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 149
(11,996 Views)

## Re: Regular Expressions Board

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 149
(11,948 Views)