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

Regex versus substring, quick solution to grab a String range out of a string

Hi all
Just a pice of cake

How can i grab this number "107501" outoff this  "20130327_110406_10_107501.pay.v01.xml"

Please note the number between 10_*.pay can grow and shrink.
I.e the number can start with 1 and grow to 99999999

The area I need to grab with Regex or substring is between _*******.pay.v01.xml
I need to grab what ever is in this are marked ***** only

Thanks in Advance
0
ZURINET
Asked:
ZURINET
  • 4
  • 3
  • 3
  • +2
1 Solution
 
Rgonzo1971Commented:
HI,

Maybe
string s = "20130327_110406_10_107501.pay.v01.xml";
string shortS  = s.Substring(0, s.Length - 12);
string[] words = shortS.Split('_');
string lastItem = words[words.Length - 1];

Open in new window

Regards
0
 
Dan CraciunIT ConsultantCommented:
Or, using regex:

.*_(\d+)\.pay

Open in new window

Will capture your number in group 1.
0
 
Derek JensenCommented:
@Dan, is that a pipe or a backslash? Looks funny on my mobile. Surround w/code tags to make more readable. :-)
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!

 
Bill PrewCommented:
Just for variety, here's another fairly simple approach.

s = "20130327_110406_10_107501.pay.v01.xml"
f = Split(Split(s, ".")(0), "_")(3)

Open in new window

~bp
0
 
Derek JensenCommented:
@bill, have you tested that? That's a very elegant solution if it works. :-)
I'd also suggest running f through a "^\d+$" regex, just to validate it's all digits. :-)
0
 
Dan CraciunIT ConsultantCommented:
Looks like should work. But it hardcodes the number of "_" and it makes it less expandable for different strings.
0
 
Bill PrewCommented:
Yes, I did test it. And yes, it does assume a format to the data to parse, but I guess all solutions have to do that in one way or another.

If the number of "_" can change that is fairly easily handled as:

a = Split(Split(s, ".")(0), "_")
f = a(UBound(a))

Open in new window

~bp
0
 
Bill PrewCommented:
I should note that my approach was prototyped in VB script, since scripting languages was one of the zones the question was posted in.

~bp
0
 
Bill PrewCommented:
And another approach in VBS which could be adapted to other languages:

f = Left(s, Instr(s, ".")-1)
f = Mid(f, InStrRev(f, "_")+1)

Open in new window

~bp
0
 
Derek JensenCommented:
Thus the beauty of Regex: nearly every single language in existence has support for it in one form or another. :-)
0
 
ZURINETAuthor Commented:
Hi all

This regex is not doing what is suppose to do :-(   .*_(\d+)\.pay

Thanks in

Advance
0
 
Dan CraciunIT ConsultantCommented:
Why?
What's the string where it fails and what's the output of $1?
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.

  • 4
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now