Solved

Reg Ex Help

Posted on 2014-11-09
7
67 Views
Last Modified: 2014-12-02
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
Comment
Question by:PlumInternet
7 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 40431627
Have you tried:

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

Open in new window

0
 

Author Comment

by:PlumInternet
ID: 40431697
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
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 40431777
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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

Author Comment

by:PlumInternet
ID: 40431804
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
 
LVL 24

Accepted Solution

by:
NVIT earned 500 total points
ID: 40432020
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
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 40432490
This regex (finally :) matches the example exactly, at the start of the line.

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

Repeat the following pattern for as many matches you need -[^\[]+\[([^\]]+)\]
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40448688
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
RegEx numeric validation 2 51
remove string in script sql 10 87
Need some help with regex 3 38
How to parse the string and split the sub directory names in asp.net 7 41
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question