DEV Community

Carlos Augusto de Medeir Filho
Carlos Augusto de Medeir Filho

Posted on

1

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.

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay