[英]regex problem C#
const string strRegex = @"(?<city_country>.+) ?(bis|zu)? (?<price>[\d.,]+) eur";
searchQuery = RemoveSpacesFromString(searchQuery);
Regex regex = new Regex(strRegex, RegexOptions.IgnoreCase);
Match m = regex.Match(searchQuery);
ComplexAdvertismentsQuery query = new ComplexAdvertismentsQuery();
if (m.Success)
{
query.CountryName = m.Groups["city_country"].Value;
query.CityOrAreaName = m.Groups["city_country"].Value;
query.PriceFrom = Convert.ToDecimal(1);
query.PriceTo = Convert.ToDecimal(m.Groups["price"].Value);
}
else
return null;
return query;
my search string is "Agadir ca. 600 eur" but "ca."我的搜索字符串是“Agadir ca. 600 eur”,但“ca.” is not "bis" or "zu" and regex is also true.不是“bis”或“zu”,正则表达式也是如此。 What is wrong with regex?正则表达式有什么问题? I want that regex is true only if is word "bis" or "zu".我希望正则表达式只有在单词“bis”或“zu”时才为真。
I think this is worng我认为这是破旧的
?(bis|zu)?
Agadir ca.
becomes your city_country
and (bis|zu)?
成为你的city_country
和(bis|zu)?
part is skipped as you've marked it as not required with ?
部分被跳过,因为您已将其标记为不需要?
. .
It looks like it's because you made bis and zu optional.看起来是因为您将 bis 和 zu 设为可选。 Try changing (bis|zu)?
尝试更改(bis|zu)?
to (bis|zu)
到(bis|zu)
Remove the question mark in (bis|zu)?
去掉(bis|zu)?
. . As it is right now, the .+
of <city_country>
matches up to the prices and includes ca.
就像现在一样, <city_country>
的.+
与价格相匹配,包括ca.
. .
In fact, you might want to change the whole ?(bis|zu)?
实际上,您可能想更改整个?(bis|zu)?
part to ( bis| zu)
.部分到( bis| zu)
。
(?<city_country>.+) ?(bis|zu)? (?<price>[\d.,]+) eur
^ ^
1 2
The first ?
第一个?
belongs to the space before属于之前的空间
The second ?
第二个?
belongs to the (bis|zu)
属于(bis|zu)
The ?
?
in these two cases makes the expression before optional在这两种情况下,使前面的表达式成为可选
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.