Solved

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

Posted on 2014-01-28
12
260 Views
Last Modified: 2014-02-04
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
Comment
Question by:ZURINET
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39814486
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
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39814592
Or, using regex:

.*_(\d+)\.pay

Open in new window

Will capture your number in group 1.
0
 
LVL 9

Expert Comment

by:Derek Jensen
ID: 39814751
@Dan, is that a pipe or a backslash? Looks funny on my mobile. Surround w/code tags to make more readable. :-)
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39814775
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
 
LVL 9

Expert Comment

by:Derek Jensen
ID: 39814794
@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
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39814802
Looks like should work. But it hardcodes the number of "_" and it makes it less expandable for different strings.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 51

Expert Comment

by:Bill Prew
ID: 39814821
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
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39814828
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
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39814835
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
 
LVL 9

Expert Comment

by:Derek Jensen
ID: 39819551
Thus the beauty of Regex: nearly every single language in existence has support for it in one form or another. :-)
0
 

Author Comment

by:ZURINET
ID: 39831893
Hi all

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

Thanks in

Advance
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39832024
Why?
What's the string where it fails and what's the output of $1?
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now