0

I have two sentences like these:

  1. "How is the weather of Paris in next day"
  2. "How is the weather of Paris next day"

I want to extract "Paris" as the city name from both of them. I wrote this regular expression:

"How is the weather of (?P<city>[^.؟!?]*) (in next day|next day)"

This expression extracts "Paris in" for the first sentence which is not right!

I want to match an input sentence with my regular expression in a way that it will be matched with the longest alternative of the second group, so I can get the shortest city name. (I thought the order of alternative in the second group matter so I put "in next day" before "next day" but It seems that the order doesn't work!) How can I rewrite my regular expression to solve this problem?

Sara Fahim
  • 177
  • 2
  • 10
  • 1
    `(?P[^.؟!?]*?)` maybe. This will make the * non greedy so should achieve the same wanted goal I guess. – Eraklon Feb 16 '20 at 08:13

1 Answers1

1

You should use lazy quantifier, which matches as few characters as possible. In your example, you should add ? in the end first group:

"How is the weather of (?P[^.؟!?]*?) (in next day|next day)"
Ali Zarezade
  • 871
  • 9
  • 22