0
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;

ca. must be for example only 1 times but the word "Agadir ca. ca. 600 eur" is also correct even if "ca." is 2 times. Why? i do not use + or ?

2 Answers 2

2

As with previous topic it gets into city_country group. Try to replace (?<city_country>.+) with (?<city_country>[^.]+). It will match everything except .. I guess your city_country couldn't have dots inside?

Sign up to request clarification or add additional context in comments.

3 Comments

This is corret. Can you explain me little better. Thx. Why [^.]+
Well, your case just says - I match any count (+ at the end) of any character (. matches one any char). My example says - I match any count of any character except .. Because . in square brackets loose its special meaning and ^ means "anything except characters in braces".
Or use even more restrictive regex for your city_country. As Pop suggested below, for example. But his example doesn't match spaces. It could be a problem, but it is your decision. I don't know your details.
1

. (Dot) Mathes anything in Regex even spaces which leads to the problem

So your matches are:

  1. @"(?<city_country>.+):Agadir ca.
  2. (cca|ca.|ungefähr): ca.
  3. (?<price>[\d.,]+) (eur)?:600 eur

You need to match the city name withouth using the dot, for example something like:

@"(?<city_country>[a-zA-Z]+) (cca|ca.|ungefähr) (?<price>[\d.,]+) (eur)?"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.