?
Solved

Reg Ex Help

Posted on 2014-11-09
7
Medium Priority
?
74 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
Industry Leaders: 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 25

Accepted Solution

by:
NVIT earned 1500 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
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…

765 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