const string strRegex = @"(?<city_country>.+) (cca|ca.|ungefähr) (?<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.CityOrAreaName = m.Groups["city_country"].Value;
query.CountryName = m.Groups["city_country"].Value;
query.Price = Convert.ToDecimal(m.Groups["price"].Value);
}
else
return null;
约。必须例如只有1次,但即使“ca.”大约“600加元阿加迪尔”也是正确的。是2次。为什么?我不使用+或?正则表达式问题C#
这是一致的。你能更好地解释我吗?谢谢。为什么[^。] + – senzacionale
那么,你的情况只是说 - 我匹配任何字符('.'匹配一个字符)的任何计数(末尾的'+')。我的例子说 - 我匹配任何除“。”之外的任何字符数。因为方括号中的'.'没有特殊含义,'^'的意思是“除了括号中的字符以外的任何内容”。 –
或者为您的city_country使用更严格的正则表达式。正如下面的Pop所示,例如。但他的例子不符合空格。这可能是一个问题,但这是你的决定。我不知道你的细节。 –