I’m using the following regex to match and capture the string weather in foo bar:
weather in ([a-z]+|[0-9]{5})\s?([a-zA-Z]+)?
Which will match and capture with bar being optional, and foo being able to be a city or a zip.
However, I would love to allow the user to write weather in foo for bar, since I have accidentally written this a few times myself. Is there any way to optionally capture a literal string like for without having to resort to \s?f?o?r?\s??
Put it in a non-capturing group:
(?:\sfor\s)?