3

I have a string "Page 1 of 15".

I need to get the value 15 as this value could be any number between 1 and 100. I'm trying to figure out:

  1. If regular expressions are best suited here. Considering string will never change maybe just split the string by spaces? Or a better solution.

  2. How to get the value using a regular expression.

1
  • 2
    Is the prefix always "Page 1 of xx" ? If not, add more samples and specs. Commented Oct 31, 2011 at 11:42

7 Answers 7

4

Regular expression you can use: Page \d+ of (\d+)

Regex re = new Regex(@"Page \d+ of (\d+)");
string input = "Page 1 of 15";
Match match = re.Match(input);
string pages = match.Groups[1].Value;

Analysis of expression: between ( and ) you capture a group. The \d stands for digit, the + for one or more digits. Note that it is important to be exact, so copy the spaces as well.

The code is a tad verbose, but I figured it'd be better understandable this way. With a split you just need: var pages = input.Split(' ')[3];, looks easier, but is error-prone. The regex is easily extended to grab other parts of the string in one go.

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

6 Comments

Is the Groups collection not 0 indexed?
@musefan: Yes, but the first item is the entire match.
I suspected so, but Google was on a different tab page
@musefan, Guffa is right, the first captured group is the entire match (this is equivalent to Perl $0, which is where .NET got its regex flavor from).
Nice solution. Personally, since I'm not great with RegEx, this seems more complicated and difficult to maintain than a simple String.Split() or String.Replace()
|
3
var myString = "Page 1 of 15";
var number = myString.SubString(myString.LastIndexOf(' ') + 1);

If there is a risk of whitespace at the end of the string then apply a TrimEnd method:

var number = myString.SubString(myString.TrimEnd().LastIndexOf(' ') + 1);

Comments

2

I think a simple String.Replace() is the best, most readable solution for something so simple.

string myString = "Page 1 of 15";
string pageNumber = myString.Replace("Page 1 of ", "");

EDIT:

The above solution assumes that the string will never be Page 2 of 15. If the first page number can change, you'll need to use String.Split() instead.

string myString = "Page 1 of 15";
string pageNumber = myString.Split(new string[] {"of"}, 
                                   StringSplitOptions.None).Last().Trim();

4 Comments

what happens when its page 2 of x :)
I assumed that Considering string will never change... meant that it would always be Page 1 of . I'll edit to made that clear.
When i read or hear the word "never" I always get scarred. The things that never change, change the most when the word is spoken by a customer :)
@Ivo, then my alternative method under edit should calm your fears :)
1

if the string format will never change then you can try this...

string input = "Page 1 of 15";
string[] splits = input.Split(' ');
int totalPages = int.Parse(splits[splits.Length - 1]);

Comments

1

If this is the only case you will ever have to handle, just split the string by spaces and use the parse the 4th part to an integer. Something like that will work:

string input = "Page 1 of 15";
string[] splitInput = string.Split(' ');

int numberOfPages = int.Parse(splitInput[3]);

Comments

0

In c# it should looks like this (using Regex class):

     Regex r = new Regex(@"Page \d+ of (\d+)");
     var str = "Page 1 of 15";
     var mathes = r.Matches(str);

Your resoult will be in: mathes[0].Groups[1]

Comments

0

You dont need a regex for this. Just the the index of the last space string var = "1 of100";

        string secondvar = string.Empty;
        int startindex = var.LastIndexOf(" ");
        if (startindex > -1)
        {
          secondvar =  var.Substring(startindex +1);
        }

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.