从正则表达式中提取值:c#。

时间:2021-08-12 13:23:18

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.

我有字符串“1 p2r”。我只是想用正则表达式来提取P和R的计数。我尝试了下面的代码,但是没有成功。

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

把塞巴斯蒂安

3 个解决方案

#1


3  

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

要匹配P和R之前的数字,请使用以下方法:

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
  • ([0-9]+)捕获组1的一个或多个数字
  • P matches P
  • P匹配
  • ([0-9]+) captures one or more digits to Group 2
  • ([0-9]+)获取组2的一个或多个数字
  • R matches R
  • R匹配

#2


0  

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

Hi来计算字符串中出现字符的数量,您可以使用以下代码段:

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

}

}

#3


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

#1


3  

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

要匹配P和R之前的数字,请使用以下方法:

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
  • ([0-9]+)捕获组1的一个或多个数字
  • P matches P
  • P匹配
  • ([0-9]+) captures one or more digits to Group 2
  • ([0-9]+)获取组2的一个或多个数字
  • R matches R
  • R匹配

#2


0  

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

Hi来计算字符串中出现字符的数量,您可以使用以下代码段:

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

}

}

#3


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