• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

Help with Regex

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
PhilippeRenaud
Asked:
PhilippeRenaud
  • 6
  • 6
  • 5
  • +1
2 Solutions
 
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.
0
 
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
0
 
Terry WoodsIT GuruCommented:
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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
käµfm³d 👽Commented:
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
 
PhilippeRenaudAuthor Commented:
Oh yes following gr it could have spaces in the text.

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

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

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

-?
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 6
  • 6
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now