Home > Software design >  Regex use of .*
Regex use of .*

Time:03-25

I understand that .* means match 0 or more of any character. But how come the regex expression [aeiou].*ing is able to match the word morning?

According to my understanding, o will be matched through the [aeiou] part then there is an r which will correspond to . but now we have an n which shouldn't get matched with .*because it should ideally match the pattern r, rr, rrr, ... But still the morning passes the regex test.

What am I getting wrong here?

CodePudding user response:

The regular expression [aeiou].*ing means "one vowel, followed by any symbol any number of times, followed by ing." It will match the orning part of morning because

  • o - matches [aeiou]
  • rn - matches .*
  • ing - matches exactly

It's true that a . by itself will match any single character, but when combined with * it doesn't mean that single character has to be repeated. Don't think of .* as just "any symbol any number of times". Think of it as "(any symbol) (any number of times)", because the (any symbol) part is allowed to change.

CodePudding user response:

.* is not a special regex combination. It is . followed by *.

. matches any character.
* does not match anything, it is a modifier. Its meaning is "repeat the previous sub-expression, zero or more times."

It does not mean the fragment of the string matched by the previous sub-expression but the sub-expression itself.

All in all, .* means "repeat . (i.e. match any character) zero or more times".
.* matches any string of any length, including the empty string (the string of length 0).

CodePudding user response:

.* means 0 or more of any character, not only a single character.

Meaning .* will match any string.

  • Related