DEV Community

Carlos Augusto de Medeir Filho
Carlos Augusto de Medeir Filho

Posted on

The powerful yet dangerous .*

Learning a bit about . * ? in Regex

In a regular expression (regex), the . (dot) character is a wildcard that matches any single character, the * (star) character indicates that the preceding character or pattern should be matched zero or more times, and the ? (question mark) character indicates that the preceding character or pattern is optional and should be matched zero or one time.

Here are some examples to illustrate the meaning of each of these characters:

  • The . character is a wildcard that matches any single character. For example, the regex a.c would match the strings abc, a9c, a#c, and so on.

  • The * character indicates that the preceding character or pattern should be matched zero or more times. For example, the regex a.*c would match the strings ac, abc, a9c9c, and so on.

  • The ? character indicates that the preceding character or pattern is optional and should be matched zero or one time. For example, the regex colou?r would match the strings color and colour, but not the string colouur.

Here are some examples that combine these characters in different ways:

  • The .* pattern is a wildcard that matches any character (.) zero or more times (*). This pattern is often used to match any characters that come before or after a specific pattern. For example, the regex abc.*def would match the strings abcdef, abc123def, and so on.

  • The .*? pattern is a non-greedy version of the .* pattern, which means that it will try to match as few characters as possible.

The a.*b? regex would match the strings a, ab, abc, and so on. The b? pattern is optional, so it will match either b or the empty string.

Top comments (0)