BreakPoint

cancel
Showing results for 
Search instead for 
Did you mean: 

Regular Expressions Board

Regulex (http://jex.im/regulex) is a good new regex visualizer. Here is a smattering of example visualizations selected from this thread:

 

Basic regex from http://forums.ni.com/t5/BreakPoint/Regular-Expressions-Board/m-p/2705169/highlight/true#M24349

 

Backreferences are supported as in http://forums.ni.com/t5/BreakPoint/Regular-Expressions-Board/m-p/2583449/highlight/true#M23742

 

 

Note! Javascript-flavored regexes only implement a subset of PCRE (LabVIEW-style) regexes. The following regex shows proper look-ahead support, but does not interpret \z end of subject as a reserved token as with PCRE. For clarity, this is a Javascript-flavor regex deficiency, not a problem with Regulex. From http://forums.ni.com/t5/BreakPoint/Regular-Expressions-Board/m-p/2895480/highlight/true#M25358

 

Javascript does not support lookbehind at all, as with(?<=\[ABC\]\[)(.*?)(?=\]) from http://forums.ni.com/t5/BreakPoint/Regular-Expressions-Board/m-p/2613621/highlight/true#M23894 -- for this reason, Regulex throws a syntax error.

 

Here's a list of features not supported by Javascript regexes, so be mindful using Regulex to design or debug PCRE-style regular expressions in LabVIEW!

Wirebird Labs: Expert Toolkits for LabVIEWDeploy, by Wirebird Labs: Expert Toolkits for LabVIEW
Message 141 of 149
(1,896 Views)

Needed to filter escape codes received over RS232 from a linux box shell response

 

Used Search and Replace with regex enabled (right-click), replace all and multiline set to true to clean in one swoop.

 

\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]

 

Replace Terminal Escape Codes.png 

 

Regex from StackOverflow (http://serverfault.com/questions/71285/in-centos-4-4-how-can-i-strip-escape-sequences-from-a-text-fi...

 

Before:

-rw-r--r--    1 root     root        34816 Jan 13 18:10 1k_symphonyconfig.xml
-rw-r--r--    1 root     root            5 Jan 13 14:10 TZ
-rw-r--r--    1 root     root            4 Jan 13 14:10 TZ_geoname
-rw-rw-rw-    1 root     root        63362 Jan 13 14:10 backupconfig.xml
-rw-r--r--    1 root     root            0 Jan 13 19:06 badsymconfig.xml
-rw-r--r--    1 root     root          155 Jan 13 15:29 group
-rw-rw-rw-    1 root     root           13 Jan 13 14:09 last_reboot_reason.txt
-rw-r--r--    1 root     root          143 Jan 13 15:30 passwd
-rw-r--r--    1 root     root          107 Jan 13 15:30 passwd.old
-rw-r--r--    1 nobody   nogroup         0 Jan 13 15:29 postLoginMessage.txt
-rw-r--r--    1 nobody   nogroup       904 Jan 13 15:29 preLoginMessage.txt
-rw-r-----    1 root     root          180 Jan 13 14:10 shadow
-rw-r-----    1 root     root          180 Jan 13 14:10 shadow.old
-rw-r--r--    1 root     root         1024 Jan 13 15:28 symphonyconfig.sec
-rw-r--r--    1 root     root        34816 Jan 14 07:37 symphonyconfig.xml
-rw-r--r--    1 root     root        61905 Jan 13 18:32 symphonyconfig.xml.postupgrade
-rw-r--r--    1 root     root            0 Jan 13 14:09 symphonyconfig.xml.preupgrade
-rw-r--r--    1 root     root          106 Jan 13 14:09 upgraderlog.txt

 

After

 

-rw-r--r--    1 root     root        34816 Jan 13 18:10 1k_symphonyconfig.xml
-rw-r--r--    1 root     root            5 Jan 13 14:10 TZ
-rw-r--r--    1 root     root            4 Jan 13 14:10 TZ_geoname
-rw-rw-rw-    1 root     root        63362 Jan 13 14:10 backupconfig.xml
-rw-r--r--    1 root     root            0 Jan 13 19:06 badsymconfig.xml
-rw-r--r--    1 root     root          155 Jan 13 15:29 group
-rw-rw-rw-    1 root     root           13 Jan 13 14:09 last_reboot_reason.txt
-rw-r--r--    1 root     root          143 Jan 13 15:30 passwd
-rw-r--r--    1 root     root          107 Jan 13 15:30 passwd.old
-rw-r--r--    1 nobody   nogroup         0 Jan 13 15:29 postLoginMessage.txt
-rw-r--r--    1 nobody   nogroup       904 Jan 13 15:29 preLoginMessage.txt
-rw-r-----    1 root     root          180 Jan 13 14:10 shadow
-rw-r-----    1 root     root          180 Jan 13 14:10 shadow.old
-rw-r--r--    1 root     root         1024 Jan 13 15:28 symphonyconfig.sec
-rw-r--r--    1 root     root        34816 Jan 14 07:39 symphonyconfig.xml
-rw-r--r--    1 root     root        61905 Jan 13 18:32 symphonyconfig.xml.postupgrade
-rw-r--r--    1 root     root            0 Jan 13 14:09 symphonyconfig.xml.preupgrade
-rw-r--r--    1 root     root          106 Jan 13 14:09 upgraderlog.txt

 

 


Now is the right time to use %^<%Y-%m-%dT%H:%M:%S%3uZ>T
If you don't hate time zones, you're not a real programmer.

"You are what you don't automate"
Inplaceness is synonymous with insidiousness

Message 142 of 149
(1,811 Views)

Does anyone know how to configure a regex for parsing a string number from 00.01 to 20.00,

 

I tried... ^([0-1][0-9]\.[0-9][1-9]|20\.00)$ but it fails when string number is i.e. 00.10 or 10.50. 

 

What can be a better regex for this one?....Thanks

0 Kudos
Message 143 of 149
(1,729 Views)

Needed to split strings received from a linux box shell to separate the command from the response. Shell commands include modifiers and piped output to process data to execute as a single command. These are also regex control characters, so I needed to escape them.

 

Used Search and Replace with regex enabled (right-click), replace all and multiline set to true to escape in one swoop. Set replace string to \\$1 to place literal backslash (escape) in front of each found regex control character (backreference)

 

(\.|\+|\*|\?|\[|\^|\]|\$|\(|\)|\{|\}|\=|\!|\<|\>|\||\:|\-)

Shell Cmd escape String.png

 

Whew! Too many backslash and pipe chars for me to digest, but it worked 🙂

Used result string as input to Match Regular Expression as regular expression and now I could separate the response from the command.

 

Effectively what the PHP preg_quote function does.

 


Now is the right time to use %^<%Y-%m-%dT%H:%M:%S%3uZ>T
If you don't hate time zones, you're not a real programmer.

"You are what you don't automate"
Inplaceness is synonymous with insidiousness

Message 144 of 149
(1,366 Views)

I'm working on a problem where I am given a function prototype for some C function and am trying to find the name of the variable we will be passing that is a specific, known, type. As an example, for the function prototype below, if I was given the input TESTSTRUCT I would want the function to return "capturedName"

 

int DoThing(TESTSTRUCT *capturedName,char *otherParameter, double yetAnotherParameter);

I was able to come up with a RegEx which seems to do this for me but I would appreciate any input on how I could simplify what I am doing or cases where my approach may give unexpected results. For the RegEx below, TESTSTRUCT will be replaced with whatever variable type I am looking for.

 

(?<=[(|\s|,])TESTSTRUCT[\s*|\**]+(\S+?)\s*[,|)]{1}

 

 

Spoiler

(?<=[(|\s|,]) - Variable name must be preceeded by comma, parentheses, or space. Prevents type "name" matching "othername"

[\s*|\**]+ - Some combination of * and whitespace between type and variable name (must have at least one of these)
(\S+?) - capture group of 1 or more non-whitespace characters (lazy)
\s* - 0 or more whitespace characters
[,|)]{1} - a comma or end parentheses

 

In my head I figured this would be a pretty regular pattern but C (CVI at least) seems lenient as far as having (or not having) white space between a lot of definitions.

Matt J | National Instruments | CLA

GCentral
0 Kudos
Message 145 of 149
(1,132 Views)

I had a quick go at simplifying the regex (tested with LabVIEW's Match Regular Expression):

\(.*TESTSTRUCT[\s\*]+(\w+)

where:

\(.*  - Match the opening parenthesis, followed by zero or more characters

TESTSTRUCT  - The type required

[\s\*]+  - One or more space or pointer characters between type and name

(\w+)  - The variable name comprising one or more alphanumeric characters

 

I've omitted the closing parenthesis, assuming the function prototype is valid. Do you need to check for prototype validity?




Certified LabVIEW Architect
Unless otherwise stated, all code snippets and examples provided
by me are "as is", and are free to use and modify without attribution.
Message 146 of 149
(1,091 Views)

@MichaelBalzer wrote:

 

I've omitted the closing parenthesis, assuming the function prototype is valid. Do you need to check for prototype validity?


For my use case, I am assuming that the function prototype is valid.

 

I didn't actually know that \w captured numbers and underscores so I will substitute \s in my original regex for \w. That also lets me omit the last part of my regex which is searching for commas or parentheses which made sure they weren't captured as part of the variable name.

 

One issue that your regex does have is that it would capture any variable names whose types end with the key word. As an example, the regex would find "capturedName" for both function prototypes below.

 

int DoThing(TESTSTRUCT *capturedName,char *otherParameter, double yetAnotherParameter);
int DoThing(notTESTSTRUCT *capturedName,char *otherParameter, double yetAnotherParameter);

I haven't tested it fully but right now I'm thinking of combining the start of my original search with the end of your for something like (?<=[(|\s|,])TESTSTRUCT[\s\*]+(\w+)

Matt J | National Instruments | CLA

GCentral
Message 147 of 149
(1,067 Views)

@Jacobson wrote:

One issue that your regex does have is that it would capture any variable names whose types end with the key word. As an example, the regex would find "capturedName" for both function prototypes below.

Good point, I didn't consider that scenario.

 

An alternative would be to match one or more non-word characters right before TESTSTRUCT, so something like:

\W+TESTSTRUCT[\s\*]+(\w+)

 

You could even have the following (note the second \W)

\W+TESTSTRUCT\W+(\w+)

 

FYI the range square brackets implicitly OR the contents, so the pipe char isn't needed. Saves a few chars, though not sure if it helps with readability!




Certified LabVIEW Architect
Unless otherwise stated, all code snippets and examples provided
by me are "as is", and are free to use and modify without attribution.
Message 148 of 149
(1,048 Views)

@MichaelBalzer wrote:

 

FYI the range square brackets implicitly OR the contents, so the pipe char isn't needed. Saves a few chars, though not sure if it helps with readability!


I didn't know that but I'll probably keep the pipe because I'll most likely forget this fact.

 

I'm going to do what I should have done at the start and just make an actual unit test to try these out but I think the last regex you posted may pick up the type of parameter N+1 if the name of the parameter N matches.

Matt J | National Instruments | CLA

GCentral
Message 149 of 149
(1,034 Views)