Help with Regex

Hello EE,

I have a string like this :


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

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Billy RothTech Team VolunteerCommented:
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.
PhilippeRenaudAuthor Commented:
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
Terry WoodsIT GuruCommented:
Maybe replace:

I think that works out to be this in VB.NET:
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

käµfm³d 👽Commented:

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.
PhilippeRenaudAuthor Commented:
Oh yes following gr it could have spaces in the text.

but not after the underscore
PhilippeRenaudAuthor Commented:
Im gonna try soon guys im via iphone right now
Terry WoodsIT GuruCommented:
Modifying kaufmed's code, this might work for you then:
Dim m As Match = Regex.Match(input, "(?<=GL_)\d+-\d+|(?<=GR_)[^)]+\)")
käµfm³d 👽Commented:
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.
Terry WoodsIT GuruCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Terry WoodsIT GuruCommented:
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.
käµfm³d 👽Commented:
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.
Terry WoodsIT GuruCommented:
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.
käµfm³d 👽Commented:
Yeah, I was mistaken, but I don't think for the reason you mention. What I was describing was:


...but that doesn't even make sense  : \
Terry WoodsIT GuruCommented:
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
Billy RothTech Team VolunteerCommented:
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.
PhilippeRenaudAuthor Commented:
PhilippeRenaudAuthor Commented:
Oh maybe one more comment ??
im sorry but when its GL I always put a  dash between some numbers but the dash is might not have one...

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

PhilippeRenaudAuthor Commented:
käµfm³d 👽Commented:
Within the pattern and after the "GL" add:

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.