问题描述
我在查找字符串中所有出现的模式时遇到问题.
I have a problem finding all occurences of a pattern in a string.
检查此字符串:
string msg= "=?windows-1258?B?UkU6IFRyIDogUGxhbiBkZSBjb250aW51aXTpIGQnYWN0aXZpdOkgZGVz?= =?windows-1258?B?IHNlcnZldXJzIFdlYiBHb1ZveWFnZXN=?=";
我想返回2次出现(以便以后对其进行解码):
I want to return the 2 occurrences (in order to later decode them):
=?windows-1258?B?UkU6IFRyIDogUGxhbiBkZSBjb250aW51aXTpIGQnYWN0aXZpdOkgZGVz?=
和
=?windows-1258?B?IHNlcnZldXJzIFdlYiBHb1ZveWFnZXN =?="
使用以下正则表达式代码,它仅返回1次出现:完整字符串.
With the following regex code, it returns only 1 occurrence: the full string.
var charSetOccurences = new Regex(@"=\?.*\?B\?.*\?=", RegexOptions.IgnoreCase);
var charSetMatches = charSetOccurences.Matches(input);
foreach (Match match in charSetMatches)
{
charSet = match.Groups[0].Value.Replace("=?", "").Replace("?B?", "").Replace("?b?", "");
}
你知道我想念什么吗?
推荐答案
当 regexp
解析器看到.*
字符序列时,它将匹配所有内容,直到字符串,然后逐个返回,(贪心匹配).因此,为避免此问题,您可以使用非贪婪匹配或显式定义可以出现在字符串中的字符.
When regexp
parser sees the .*
character sequence, it matches everything up to the end of the string and goes back, char by char, (greedy match). So, to avoid the problem, you can use a non-greedy match or explicitly define the characters that can appear at a string.
"=\?[a-zA-Z0-9?=-]*\?B\?[a-zA-Z0-9?=-]*\?="
这篇关于正则表达式查找字符串中所有出现的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!