Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help with Regex

Posted on 2012-04-03
19
Medium Priority
?
384 Views
Last Modified: 2012-04-04
Hello EE,

I have a string like this :

GR_Hello

and could be also:

GL_21388-30 ( This is a Test )


It will always be GR or GL  at the beginin following a underscore  then after its unique

What would the regex pattern be ?

If you want to go further (if possible)  when its GL at the begenning.. I would need only the 34578-3099 number   not the parentheseses

Thanks
0
Comment
Question by:PhilippeRenaud
[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
  • 6
  • 6
  • 5
  • +1
19 Comments
 
LVL 5

Expert Comment

by:Billy Roth
ID: 37803402
Let me get this straight.  there is only 1 delimiter which is a space, followed by text in parenthesis in which it is captures as text where the spaces are not delimiters.  we want the "number" following Gl_ to be handled as the one and only replaceable variable?  if this is the case I already use a very similar expression, I am on the road but I can copy that to you later this evening.
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 37803441
in the case of GL_

yes there is a space after the number like this :    GL_1000-22 ( .... )    space after 22
then parentheses and inside them, some text

but I would like to retrieve the number only... (of course I could substring the string before going to Regex) if its less complicated for you..

in case of GR_
only some text
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 37803705
Maybe replace:
^G(?:L_.*?(\d+-\d+).*|R_(.*))
with
\1\2

I think that works out to be this in VB.NET:
regex.Replace(sourcestring,"^G(?:L_.*?(\d+-\d+).*|R_(.*))","\1\2")
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 1000 total points
ID: 37804090
Perhaps:

Imports System.Text.RegularExpressions

...

Dim value As String = String.Empty
Dim m As Match = Regex.Match(input, "(?<=GL_)\d+-\d+|(?<=GR_)[^ ]+")

If m.Success Then
    value = m.Value
End If

Open in new window


If the string following GR can contain spaces, then the pattern can be adjusted to accommodate.
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 37804195
Oh yes following gr it could have spaces in the text.

but not after the underscore
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 37804199
Im gonna try soon guys im via iphone right now
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 37804232
Modifying kaufmed's code, this might work for you then:
Dim m As Match = Regex.Match(input, "(?<=GL_)\d+-\d+|(?<=GR_)[^)]+\)")
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37804245
I'm wondering if that modification is needed, though. I don't quite understand the "but not after the underscore" comment. If by that we mean that the thing being "captured" is the string of characters following the underscore, but up to (not including) the first space, then no modification is required. If, however, we want everything after the underscore, then perhaps Terry's mod would be appropriate. My concern, though, is that there doesn't appear to be any mention of "GR" strings containing parentheses.
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 1000 total points
ID: 37804295
I think I was getting mixed up between the GL and GR syntax actually. To just match everything after the GR (including any spaces), this would make sense:
Dim m As Match = Regex.Match(input, "(?<=GL_)\d+-\d+|(?<=GR_).*")

Again, this is based on kaufmed's code, so if you accept my solution, please split the points.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 37804299
And to require the GL or GR to be at the start of the line, this would be needed I think:
Dim m As Match = Regex.Match(input, "(?<=^GL_)\d+-\d+|(?<=^GR_).*)")

I'm assuming the ^ will successfully match the start of the string in .NET without trouble - I have had trouble with that kind of usage in PHP before.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37804309
I'm assuming the ^ will successfully match the start of the string in .NET without trouble
It should, AFAIK. To be certain you match the beginning of the string, you could move the caret to outside of a non-capturing group which surrounds the main pattern.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 37804321
Initially I thought so kaufmed, but actually you'd have to start using captured subpatterns for you result instead of the complete match if you did that, as we don't want to capture the G[RL]_ which comes after the start of line position.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37804339
Yeah, I was mistaken, but I don't think for the reason you mention. What I was describing was:

^(?:(?<=GL_)\d+-\d+|(?<=GR_).*))

...but that doesn't even make sense  : \
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 37804344
If you were really quick at loading the page earlier, you would've seen I suggested that exact pattern already, but quickly edited it out again! haha
0
 
LVL 5

Expert Comment

by:Billy Roth
ID: 37804564
Simple.. Tested using notepad++ for a quick test. (I have not had a chance to test in Visual Studio, but notepad++ has very microsoft like regular expression usage)

search (GR_|GL_)([-a-zA-Z0-9|\.|_|/]*)(\s)
replace \1\2\3

You have 3 pieces of information, GR_ or GL_ indicates the beginning of your search and is mapped to \1. Your content is mapped to variable  \2.  a space indicates the end which is mapped to \3.

To make GR_Hello convert to GR_HELLOWORLD run the replace like below as an example.  Whatever you need to do i am not certain, but i believe you want to do something with the data in variable \2.
search (GR_|GL_)([-a-zA-Z0-9|\.|_|/]*)(\s)
replace \1HELLOWORLD\3

NOTE: also you may notice I matched for chars like . _ / in the string as an example, you can take them out as you need.
0
 
LVL 1

Author Closing Comment

by:PhilippeRenaud
ID: 37805932
=)
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 37805970
Oh maybe one more comment ??
im sorry but when its GL I always put a  dash between some numbers but the dash is optional..it might not have one...

what would be the little modificaiton to do for thaT? (without asking a new question ..)

thanks...
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37806025
Within the pattern and after the "GL" add:

-?
0

Featured Post

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!

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

610 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