Solved

Reg Ex Help

Posted on 2014-11-09
7
60 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 23

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 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now