請採取以下字符串:「互聯網上的營銷和板球」。使用正則表達式查找所有匹配 - 貪婪和非貪婪!
我想使用正則表達式找到「Ma」-any text-「et」的所有可能匹配項。所以..
- 市場
- 營銷和板球互聯網
正則表達式Ma.*et
回報 「在互聯網上營銷和Cricket」 關於
Ma.*?et
返回市場。但我想要一個返回所有3的正則表達式。這可能嗎? 謝謝。
請採取以下字符串:「互聯網上的營銷和板球」。使用正則表達式查找所有匹配 - 貪婪和非貪婪!
我想使用正則表達式找到「Ma」-any text-「et」的所有可能匹配項。所以..
正則表達式Ma.*et
回報 「在互聯網上營銷和Cricket」 關於
Ma.*?et
返回市場。但我想要一個返回所有3的正則表達式。這可能嗎? 謝謝。
據我所知:
不可以,但你可以匹配非貪婪,然後再生成一個量詞一個新的正則表達式來獲得的第二場比賽。 像這樣:
Ma.*?et
Ma.{3,}?et
...等等...
不幸的是,這是不可能與標準的POSIX正則表達式,它返回一個(最好的候選人,每正則表達式規則)匹配。假設您正在程序中使用它,您將需要利用擴展功能,該功能可能以您使用此正則表達式的特定編程語言存在,以完成此任務。
感謝球員,真正幫助。以下是我想出了PHP:
function preg_match_ubergreedy($regex,$text) {
for($i=0;$i<strlen($text);$i++) {
$exp = str_replace("*","{".$i."}",$regex);
preg_match($exp,$text,$matches);
if($matches[0]) {
$matched[] = $matches[0];
}
}
return $matched;
}
$text = "Marketing and Cricket on the Internet";
$matches = preg_match_ubergreedy("@Ma.*[email protected]",$text);
對於一個更一般的正則表達式,另一種選擇是遞歸對陣以前匹配的貪婪正則表達式,反過來丟棄第一和最後一個字符,以確保你只匹配上一場比賽的一個子串。匹配Marketing and Cricket on the Internet
後,我們測試了子域匹配arketing and Cricket on the Internet
和Marketing and Cricket on the Interne
。
它去在C#這樣的事情...
public static IEnumerable<Match> SubMatches(Regex r, string input)
{
var result = new List<Match>();
var matches = r.Matches(input);
foreach (Match m in matches)
{
result.Add(m);
if (m.Value.Length > 1)
{
string prefix = m.Value.Substring(0, m.Value.Length - 1);
result.AddRange(SubMatches(r, prefix));
string suffix = m.Value.Substring(1);
result.AddRange(SubMatches(r, suffix));
}
}
return result;
}
這個版本可以,但是,最終多次返回相同的子匹配,例如,它會發現Marmoset
兩次Marketing and Marmosets on the Internet
,首先作爲一個子匹配的Marketing and Marmosets on the Internet
,然後作爲Marmosets on the Internet
的子匹配。
恩,你真的需要正則表達式嗎? – Gumbo 2010-11-03 21:03:52
LEPL是一個用於Python的解析庫,它具有「產生」所有可能匹配的正則表達式。 – delnan 2010-11-03 21:07:13