Click here to Skip to main content
15,906,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,
I have a string like this.
"This is a sample string with 215[145] - Microsoft Word"
And sometimes, there is possibility to have a string like this.
"This is a sample string with 85 - Microsoft Word"

In both cases, i want to extract the numbers. To be precise , i want "215" from first string and "85" from second string.
So i planned to use regex. This is my pseudo regex -
"Left side of the string must be a space, Then only catch numbers atleast one digit to maximum 3 digts, Then end position must be a space or a "[" sign.
How to tell regex to get my string ?

What I have tried:

VB
"\s[0-9]{1,3}[^[\s]]"
Posted
Updated 24-Jan-17 1:21am
Comments
Richard MacCutchan 24-Jan-17 6:48am    
So what is the problem; does it work or not?
Vinod Kc 24-Jan-17 7:11am    
Hi, Thanks for the reply. But it is catching a space in the beginning of the string. How to avoid it ?
I mean, this is the result now - " 85" and " 215"
Vinod Kc 24-Jan-17 7:00am    
I got this from RegexBuddy.
"\s\d+([^\[\s])"

Try:
(?<=^[^\d]*)\d+(?=[^\d]*.*$)

It treats anything up to the first digit as an excluded prefix, matches all contiguous digits, then treats the rest of the line as an excluded suffix.
 
Share this answer
 
Comments
Vinod Kc 24-Jan-17 7:15am    
Thanks for the reply, But it is not working in RegexBuddy.
OriginalGriff 24-Jan-17 7:31am    
It works for me when I test it in Expresso - so check your data and exactly how you are using it.
Not a solution, just a few links ti help you to understand/build/debug RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
 
Share this answer
 
Comments
Vinod Kc 24-Jan-17 8:24am    
Hi, Thanks for the links. But Debuggex seems to be complex. Anyhow, i am playing with it. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900