2

I have string "1P2R". I just want to extract count of P & R from it using regular expression. I tried the following codes, but didn't work.

String regex = "[0-9]+[P]?[0-9]+[R]?";
String input = "1P2R";
MatchCollection coll = Regex.Matches(input, regex);
String result = coll[0].Groups[1].Value;

or

String regex = "[0-9]+[P]?[0-9]+[R]?";
String input = "1P2R";
Match match = Regex.Match(input, regex);
if (match.Success)
  {
      string key = match.Value;
  }

Both methods were not giving the result. How I can achieve this?

I have changed the code as follows...

String regex = "[0-9]+[P]?[0-9]+[R]?";
String input = "1P2R";
Match match = Regex.Match(input, regex);
if (match.Success)
{
string a, b;
a = input.Substring(0, input.LastIndexOf("P"));
b = input.Substring(input.LastIndexOf("P") + 1, input.LastIndexOf("R") - input.LastIndexOf("P")-1);
}

is it ok?

Regards Sebastian

Sebastian Xavier
  • 2,499
  • 7
  • 27
  • 41
  • How do you want to extract count of `P` and `R`? Dou you mean chars count? If yes, dou have to use regex? –  Jul 24 '14 at 06:23
  • you need to capture `P` and `R`.`String regex = "[0-9]+([P])?[0-9]+([R])?";` – Avinash Raj Jul 24 '14 at 06:24

3 Answers3

3

To match the numbers before P and R, use this:

var myRegex = new Regex(@"([0-9]+)P([0-9]+)R");
var myMatch = myRegex.Match(yourString);
string pCount = myMatch.Groups[1].Value;
string rCount = myMatch.Groups[2].Value;
Console.WriteLine(pcount,rcount);

Explanation

  • ([0-9]+) captures one or more digits to Group 1
  • P matches P
  • ([0-9]+) captures one or more digits to Group 2
  • R matches R
zx81
  • 41,100
  • 9
  • 89
  • 105
  • One issue.. This is working for string "2P1R", if we reverse it : say "2R1P", it is not working. – Sebastian Xavier Jul 24 '14 at 09:22
  • If you want it to be either `P` or `R`, use `@"([0-9]+)[PR]([0-9]+)[PR]"` – zx81 Jul 24 '14 at 09:36
  • Good info, But one Problem: Value of P gets assigned to R & vice versa. – Sebastian Xavier Jul 24 '14 at 09:55
  • I see what you're trying to do now. Then we have to use a different approach: `@"(?=.*([0-9]+)P)(?=.*([0-9]+)R)"` – zx81 Jul 24 '14 at 11:24
  • For an explanation of the comment just above, please see [this answer](http://stackoverflow.com/a/24472179/1078583) that I wrote some time ago—same technique. :) – zx81 Jul 24 '14 at 11:26
  • I have an additional query on this. I want to know which is the first character, is it 'P' or 'R'. – Sebastian Xavier Jul 25 '14 at 09:57
  • I have tried the below method, but didn't work.. public static Dictionary GetSelectValuesDictionary(string inputString) { return Regex.Matches(inputString, @"(?=.*([0-9]+)P)(?=.*([0-9]+)R)") .Cast() .ToDictionary( m => m.Groups["key"].Value, m => m.Groups["value"].Value); } – Sebastian Xavier Jul 25 '14 at 10:09
  • one more finding, this expression fails, if we give values greater than 9 – Sebastian Xavier Jul 25 '14 at 10:26
  • Indeed. `@"(?=.*?([0-9]+)P)(?=.*?([0-9]+)R)"` – zx81 Jul 25 '14 at 11:47
0

Hi to count the number of occurrence of a character in a string you can use following code segment:

class countPR
{
public static void main(String[] args) 
{
    String s="1P2R1P2R1P2R1P2R1P2R1P2R1P2R1P2R";        
    Pattern p = Pattern.compile(".P");
    //  get a matcher object
    Matcher m = p.matcher(s);
    int countP = 0;
    while(m.find()) 
    {
        countP++;           
    }
    System.out.println("P found "+countP+" number of times");

    p = Pattern.compile(".R");
    m = p.matcher(s);
    int countR = 0;
    while(m.find()) 
    {
        countR++;           
    }
    System.out.println("R found "+countR+" number of times");
}

}

gkbstar
  • 615
  • 1
  • 6
  • 16
0
using System;
using System.Text.RegularExpressions;

namespace RegExApplication
{
class Program
{
  private static void showMatch(string text, string expr)
  {
     int count=0;
     Console.WriteLine("The Expression: " + expr);
     MatchCollection mc = Regex.Matches(text, expr);
     foreach (Match m in mc)
     {
        count++;
     }
     Console.WriteLine(expr+" found "+count+" number of times");
  }
  static void Main(string[] args)
  {
     string str = "1P2R1P2R1P2R1P2R1P2R1P2R1P2R1P2RGIRISHBALODI";

     Console.WriteLine("Matching words that start with 'S': ");
     showMatch(str, @".P");
     showMatch(str, @".R");
     Console.ReadKey();
  }
}
}
gkbstar
  • 615
  • 1
  • 6
  • 16