LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Regex question, pick certain parts from a string

Solved!
Go to solution

Hello,

 

I was always very bad at string manipulations and regex things, so sorry if this is too basic! 🙂 I just find always non-relevant/different examples when I check help files and search the forum.

 

So, example string:

"Blabla_UnexpectedError                       =  4,"

What is the most straightforward way to pick the "UnexpectedError" as a string, and the number as a Integer? All my strings are in similar formats, only the "Blabla" and the white spaces are varying.

Which string function would suit best, and how to use it (Regex format, can you point me to a useable reference list, which shows examples too)?

 

Thanks! 🙂

Message 1 of 9
(3,965 Views)
Solution
Accepted by topic author Blokk

This works for match regular expression, and you need to extract the bottom to get the two results (groups).

 

 

_(.*) = (\d+)

 

Previous will search the first _, then (tries to) match anything (and return it in the first group), then match \s=\s, then one or more decimal digits (second group). 

 

_(\S+) += +(\d+)

 

Previous one is a bit easier on the spaces (spaces will not be included in the groups)..

 

You can make it more strict (force string before _, check the ",", etc.), or more flexible (allow more spaces, etc.). But that's up to you (to let us know or try yourself)!

Match for Blokk.png

Message 2 of 9
(3,942 Views)

hi Blokk,

 

if using scan from sring --  %[^_]_%s =%d

bp
0 Kudos
Message 3 of 9
(3,932 Views)

This works too, but it's more difficult to make it fit your exact needs, it's less flexible. You do get an integer, instead of a string.

Scan fromString for Blokk.png

0 Kudos
Message 4 of 9
(3,931 Views)

wiebe@CARYA wrote:

This works for match regular expression, and you need to extract the bottom to get the two results (groups).

 

 

_(.*) = (\d+)

 

Previous will search the first _, then (tries to) match anything (and return it in the first group), then match \s=\s, then one or more decimal digits (second group). 

 

_(\S+) += +(\d+)

 

Previous one is a bit easier on the spaces (spaces will not be included in the groups)..

 

You can make it more strict (force string before _, check the ",", etc.), or more flexible (allow more spaces, etc.). But that's up to you (to let us know or try yourself)!

Match for Blokk.png


I generally use '\s' in my regular expressions instead of a simple space character. This allows for any whitespace as a separator rather than only a space character. You never know when a pesky tab shows up in the string.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
Message 5 of 9
(3,901 Views)

Mark_Yedinak wrote:

I generally use '\s' in my regular expressions instead of a simple space character. This allows for any whitespace as a separator rather than only a space character. You never know when a pesky tab shows up in the string.


That could be a good or a bad thing. Maybe you want a fail when there is a tab.

 

You need some basic knowledge about reg.ex.'s before the become *very* practical. There is an incredible amount of possibilities (even more in Python where you can call functions from the reg.ex.).

 

It's really a matter of knowing exactly what you require, and then extending your knowledge about reg.ex. to see how that is done.

 

I use the Python Pocket Reference (google) that I bought purely for the reg.ex. section. PDF is in GitHub. It get's interesting on page 203 ("(?i)" and "(?-I)", etc.), 207-209. The third edition has the same info, and it's only a few buck second hand. So I only use 4 pages of the book, but I keep losing print out's between 500 other print outs.

 

http://www.rexegg.com/ is great too.

 

I don't use https://regex101.com/ a lot, but the explanation section (top right) is great for learning. So it the quick reference section (bottom right).  And the reg ex debugger..

 
0 Kudos
Message 6 of 9
(3,753 Views)

I've bookmarked this website: https://www.regular-expressions.info/, and also paid the paltry fee to get a copy of their RegexBuddy Windows tool, which helps me craft and test the regex for each new parsing task.

 

(I "do" regexes just infrequently enough that the brain has to almost start over from scratch each time.)

 

Dave

David Boyd
Sr. Test Engineer
Abbott Labs
(lapsed) Certified LabVIEW Developer
Message 7 of 9
(3,737 Views)

I've submitted presentations about reg.ex. for NIDays a few times, but it's just not sexy enough to make it. I think good regex knowledge can save days of work over the career of an average developer...

 


DavidBoyd wrote:

(I "do" regexes just infrequently enough that the brain has to almost start over from scratch each time.)


That's why they're call regular expressions. You need to use then regularly.

0 Kudos
Message 8 of 9
(3,713 Views)

wiebe@CARYA wrote:

I've submitted presentations about reg.ex. for NIDays a few times, but it's just not sexy enough to make it. I think good regex knowledge can save days of work over the career of an average developer...

 


DavidBoyd wrote:

(I "do" regexes just infrequently enough that the brain has to almost start over from scratch each time.)


That's why they're call regular expressions. You need to use then regularly.


Me too. I use then extensively. I very rarely use Scan from String. I prefer to use regular expression as they are much more powerful in my opinion.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 9 of 9
(3,701 Views)