1

Let's say I have a string like:

SORT_123_456

Is there an easy way to parse the two integer values? I do not know how many digits these values will have. It would also be helpful if I could validate the other characters and just abandon the parsing if they don't appear as I have them above (that would indicate something is wrong).

I know I can parse character by character, but I was wondering if Regex could handle this.

I just haven't really used regular expressions. Can someone tell me if this can be done more easily using Regex?

5
  • Will they always be "_" delimited? Commented Sep 3, 2014 at 17:58
  • Yes, just like I have it above except for the exact digits. And if it isn't "_" delimited, then I just abandon parsing because something would be very wrong. Commented Sep 3, 2014 at 17:59
  • you could have used the split() function and then check to see if you have 3 elements in the array that are loaded if string[0] = "" then you know that the word SORT_ was missing Commented Sep 3, 2014 at 18:06
  • @DJKRAZE: Yes, that's a pretty simple approach too. Commented Sep 3, 2014 at 18:07
  • I posted an example using Split Function as well so that you could also reference or try if you are not too familiar with using RegEx Commented Sep 3, 2014 at 18:15

3 Answers 3

6

SORT_(\d+)_(\d+) will do it. Just extract the two groups after using your regex.

If SORT is remplaced by an other word, then \w+_(\d+)_(\d+) will do it, if it is totally missing, (\d+)_(\d+) will be the regex, and finally, if the word must be in Caps : [A-Z]+_(\d+)_(\d+).

Sign up to request clarification or add additional context in comments.

4 Comments

I'll try that. What happens if the "SORT_" is missing in this case?
He want a generic solution, to get the numbers in a string using regex.
I want it to fail if "SORT" is missing, because that means something is not as expected.
@JonathanWood Then the first regex will do it, it won't match at all if this isn't SORT : proof
2

If you want an example using the Split() Function here is what you could do

var splitStr = "SORT_123_456";
var sortable = splitStr.Split('_');
if (sortable[0].Contains("SORT"))
{
    //do your sorting logic because you have a sortable 
    sortable[1] //will be "123"
    sortable[2] //will be "456"
}

or you could check for string.Empty

var splitStr = "SORT_123_456";
var sortable = splitStr.Split('_');
if (!sortable[0] == string.Empty)
{
    //do your sorting logic because you have a sortable 
    sortable[1] //will be "123"
    sortable[2] //will be "456"
}

3 Comments

In fact, this is what I ended up doing because it seemed simpler and may be more performant. I've marked Carlier Robin's post as the answer though, just because it more accurately answers what I originally asked.
totally understandable I was just providing you with a different approach hence the many ways to skin a cat as we say.. :)
+1 I am totally in favor of substr/indexof solutions in trivial patterns such as this.
1

This is the simple way. One simple regular expression. It validates the source string and extracts and captures all the the numeric fields, regardless of number of such fields found:

string src = @"SORT_123_456_789" ;
Regex  rx = new Regex( @"^SORT(_\d+)*$" ) ;
Match  match = rx.Match( src ) ;

if ( !match.Success ) throw new InvalidOperationException() ;

int[] values = null ;
if ( match.Success )
{
  values = match
          .Groups[1]
          .Captures
          .Cast<Capture>()
          .Select( c => int.Parse( c.Value.Substring(1) ) )
          .ToArray()
          ;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.