正则表达式查找字符串中所有出现的模式 [英] Regex find all occurrences of a pattern in a string

查看:49
本文介绍了正则表达式查找字符串中所有出现的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在查找字符串中所有出现的模式时遇到问题.

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?=-]*\?="

这篇关于正则表达式查找字符串中所有出现的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆