Sian Jakey Ellis
Sian Jakey Ellis

Reputation: 445

Getting a value from a string using regular expressions?

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.

Upvotes: 3

Views: 187

Answers (7)

Abel
Abel

Reputation: 57149

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.

Upvotes: 4

James Hill
James Hill

Reputation: 61793

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();

Upvotes: 2

Ivo
Ivo

Reputation: 3436

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);
        }

Upvotes: 0

Kamil Lach
Kamil Lach

Reputation: 4629

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]

Upvotes: 0

Andy Rose
Andy Rose

Reputation: 16974

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);

Upvotes: 3

Florian Greinacher
Florian Greinacher

Reputation: 14786

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]);

Upvotes: 1

musefan
musefan

Reputation: 48415

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]);

Upvotes: 1

Related Questions