You are viewing limited content. For full access, please sign in.

Question

Posted to Government

Question

Help in a pattern Matching?

asked on January 11, 2024

Hello team! I want to extract a value from a file name between '-' and '_'. For example the file name is: 24-001_Daniel Vargas_1/10/2024. The value to extract would be '001' but I am having '001_Daniel Vargas' with this formula:

-(.+)_

What is wrong? I left an image like reference

Thanks in advance.

lsanswer.PNG
lsanswer.PNG (11.7 KB)
0 0

Answer

SELECTED ANSWER
replied on January 11, 2024 Show version history

Looking at your attempt, when you specify a single underscore, you're taking everything up until the last underscore within your text.  

I have two different ideas on this:
1) If you know that 3-digit number will always be the only 3-digit number within this string of text, you could use "\d{3}" to pull out the only matching 3-digit number pattern:



2) If you can't reliably say that number will always be the only 3-digit number within this text, you could do something like the following to take that specific portion out of the string of text instead:



(There might be another easier way to do this that I'm not thinking of, but either of these will work.)

0 0
replied on January 11, 2024

beautiful! I use the first option and it is working! thank you! other question: the value is store like a string, but I want the value like an integer, because I am use to make a sum. Do you know how can I storage like int?

 

0 0
replied on January 12, 2024

One way you should be able to do it is assign a token value to the value you've filtered out, and use the Token Tags to convert it to a Number?

0 0

Replies

replied on January 12, 2024 Show version history

I would probably use something like this

^[^-]+[-]([^_]+)

This will get everything after the first "-" up to but not including the first "_"

 

Or a less restrictive pattern would be

-([^_]*)

 

1 0
replied on January 12, 2024

I would use -(.*?_)_ to avoid any special assumptions. 

1 0
replied on January 12, 2024 Show version history

Fabian, 

If the value you are looking at is going to be the same format all the time (24-001_Daniel Vargas_1/10/2024), which looks like the two digit year, with a hyphen and then the number you are looking for, you could use this for the pattern matching ...(...).*   This will skip past the first three digits and then select the next three.  I use this to pull a year from a file numbering schema we use.

Steve

0 0
replied on January 12, 2024

Oops, typo -(.*?)(?=_)

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.