简体   繁体   English

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

[英]extract values from regular expression : c#

I have string "1P2R". 我有字符串“ 1P2R”。 I just want to extract count of P & R from it using regular expression. 我只想使用正则表达式从中提取P&R的计数。 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 问候塞巴斯蒂安

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

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

} }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM