从正则表达式中提取值:c#
我有字符串“1P2R”。我只想使用正则表达式从中提取P&R计数。 我尝试了以下代码,但没有成功从正则表达式中提取值:c#,c#,regex,C#,Regex,我有字符串“1P2R”。我只想使用正则表达式从中提取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; 或 两种方法都没有给出结果。我怎样才能做到这一点 我已更改代码如下 String regex
String regex = "[0-9]+[P]?[0-9]+[R]?";
String input = "1P2R";
MatchCollection coll = Regex.Matches(input, regex);
String result = coll[0].Groups[1].Value;
或
两种方法都没有给出结果。我怎样才能做到这一点
我已更改代码如下
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);
}
可以吗
问候
Sebastian要匹配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);
解释
将一个或多个数字捕获到组1([0-9]+)
匹配P
P
将一个或多个数字捕获到组2([0-9]+)
匹配R
R
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");
}
}如何提取
p
和R
的计数?你是说chars count吗?如果是,您必须使用正则表达式?您需要捕获P
和R
字符串正则表达式=“[0-9]+([P])?[0-9]+([R])?”代码>图案和匹配器不可用?它是特定于框架的吗?@SebastianXavier看起来更像Java而不是C#。。。Java使用pattern/matcher,@SebastianXavier是的,这是Java的一个代码段,您可以使用C#的follow代码段。。这对字符串“2P1R”有效,如果我们反转它:说“2R1P”,它就不起作用。如果您希望它是P
或R
,请使用@([0-9]+)[PR]([0-9]+)[PR]”
很好的信息,但有一个问题:P的值被分配给R,反之亦然。我知道您现在正在尝试做什么。然后我们必须使用另一种方法:@(?=.*([0-9]+)P)(?=.*([0-9]+)R)
要解释上面的评论,请参阅我不久前写的相同技巧。:)
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();
}
}
}