Solved

Reg Ex Help

Posted on 2014-11-09
7
73 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: 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!

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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,…
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…

705 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