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)

while "Agadir ca. 600" is false but "Agadir ca. 600 eur" is true. "eur" is optional but "Agadir ca. 600" is false? Why?

4 Answers 4

1

Enclose eur in parenthesis, like so (eur)?.

? matches the preceding character zero or one times. By enclosing it in the parens, it will match the whole word.

Also, maybe it is matching the last space before (eur)?. Try this:

@"(?<city_country>[^.]+) (cca|ca.|ungefähr) (?<price>[\d.,]+)(\seur)?"

Notice, I removed the last white space and added \s, which matches any white space including space, tab, form-feed, etc.

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

1 Comment

I think so, but testing it will give you definitive answer. :)
0

I think you should write (eur)? instead of eur?. I.e. place the Space before also inside the parenthesis

2 Comments

is the same even if i use (eur)?
I modified my answer. Place the space before also inside the parenthesis like ( eur)?.
0
@"(?<city_country>[^.]+) (cca|ca.|ungefähr) (?<price>[\d.,]+)( eur)?"

OR

@"(?<city_country>[^.]+) (cca|ca.|ungefähr) (?<price>[\d.,]+)(\seur)?"

Comments

0

Using (eur)? instead of eur? should solve the problem.

best way would be @"(?<city_country>[^.]+) (cca|ca.|ungefähr) (?<price>[\d.,]+)\s*(\seur)?") putting space only inside like ( eur) would not match something like "Agadir ca. 600 "

2 Comments

is the same even if i use (eur)?
maybe you can just try with eur? and (eur)?, without the previous part of the regex.

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.