Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 76
  • Last Modified:

Reg Ex Help

Hello,

We current have a string which looks like:

-variablename[value]-

We would like to create a regular expression that simply extracts the value from the string.

Thanks
0
PlumInternet
Asked:
PlumInternet
1 Solution
 
Tommy BraasCommented:
Have you tried:

^-[^\[]([^\]])

Open in new window

0
 
PlumInternetAuthor Commented:
Hi Tommy,

The snippet above doesn't appear to work.

So far we have been able to break our string that looks like:

-variablename1[value1]-variablename2[value2]-variablename3[value3]-

Into groups using:

^(.+)\-(.+)\-(.+)$

Which gives us:

group1: variablename1[value1]
group2: variablename2[value2]
group3: variablename3[value3]

What we are trying to do update the string above to return the "value" nested within the braces rather than  variablename[value] string.

For example:

group1: value1
group2: value2
group3: value3
0
 
Tommy BraasCommented:
That's a slightly differently problem than the original question.

My regex did indeed not work. This revised one will match the whole string up until the second dash:

^-[^\[]+([^\]])+\]

The problem with using .+ is that the . (period) is greedy and it's behaviour will differ dependent on the implementation in use.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PlumInternetAuthor Commented:
Hi Tommy,

This appears to be getting closer, however I have found that it is only returning the character before the close brace rather than all the character contained with in the braces  

For example, if our string is: variablename1[value1]
The value being returned is: 1
what we need is: value1

Thanks
0
 
NVITCommented:
This works for me in Notepad++ editor...

Original text...
-variablename1[value1]-variablename2[value2]-variablename3[value3]-

Find: ^.+\[(\b.+\b)\].+\[(\b.+\b)\].+\[(\b.+\b)\]-$
Replace: group1:\1\ngroup2:\2\ngroup3:\3

Becomes...
group1:value1
group2:value2
group3:value3
0
 
Tommy BraasCommented:
This regex (finally :) matches the example exactly, at the start of the line.

   ^-[^\[]+\[([^\]]+)\]

Repeat the following pattern for as many matches you need -[^\[]+\[([^\]]+)\]
0
 
käµfm³d 👽Commented:
It helps if we know the programming language, text editor, or website you are testing with. Regex engines vary widely in their underlying implementations, and even syntax. If you can tell us this, then drafting an appropriate regex should be much easier.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now