我试图使用以下文本获取值,任何想法可以使用正则表达式完成吗?
Lorem ipsum dolor坐%下载%#456 amet,consectetur adipiscing%download%#3434 elit.Duis non nunc nec mauris feugiat porttitor.Sed tincidunt blandit dui viverra%download%#298.Aenean dapibus nisl%download%#893434 id nibh auctor vel tempor velit blandit.
456
3434
298
893434
Run Code Online (Sandbox Code Playgroud)
提前致谢.
Jus*_*gan 63
所以你试图获取前面有令牌"%download%#"的数字值?
试试这种模式:
(?<=%download%#)\d+
Run Code Online (Sandbox Code Playgroud)
这应该工作.我不认为#
或者%
是.NET Regex中的特殊字符,但你必须要么像反斜杠那样\\
使用反斜杠,要么对整个模式使用逐字符串:
var regex = new Regex(@"(?<=%download%#)\d+");
return regex.Matches(strInput);
Run Code Online (Sandbox Code Playgroud)
在这里测试:http://rextester.com/BLYCC16700
注意: lookbehind断言(?<=...)
很重要,因为您不希望%download%#
在结果中包含仅包含其后的数字.但是,您的示例似乎在要捕获的每个字符串之前需要它.lookbehind组将确保它在输入字符串中,但不会在返回的结果中包含它.更多关于外观断言的信息.
Fir*_*oso 40
我看到的所有其他响应都很好,但C#支持命名组!
我使用以下代码:
const string input = "Lorem ipsum dolor sit %download%#456 amet, consectetur adipiscing %download%#3434 elit. Duis non nunc nec mauris feugiat porttitor. Sed tincidunt blandit dui a viverra%download%#298. Aenean dapibus nisl %download%#893434 id nibh auctor vel tempor velit blandit.";
static void Main(string[] args)
{
Regex expression = new Regex(@"%download%#(?<Identifier>[0-9]*)");
var results = expression.Matches(input);
foreach (Match match in results)
{
Console.WriteLine(match.Groups["Identifier"].Value);
}
}
Run Code Online (Sandbox Code Playgroud)
读取的代码:(?<Identifier>[0-9]*)
指定[0-9]*
结果将成为我们如上索引的命名组的一部分:match.Groups["Identifier"].Value
小智 5
public void match2()
{
string input = "%download%#893434";
Regex word = new Regex(@"\d+");
Match m = word.Match(input);
Console.WriteLine(m.Value);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
192957 次 |
最近记录: |