I'm new to regular expressions and I'm trying to come up with a regex which matches a word which contains at least one letter and zero or more digits.
Example:
users
- match
u343sers
- match
13123123
- not match
I cannot use lookarounds because I need to use the regex in Go and the regexp
package doesn't support them.
Is this possible without lookarounds?
?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).
Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" . Non-alphanumeric characters without special meaning in regex also matches itself. For example, = matches "=" ; @ matches "@" .
Regexp is built for exactly this, no need for look arounds.
The Regexp
\w*[a-zA-Z]\w*
Explanation:
\w
: Any letter or number, *
: 0 or more times[a-zA-Z]
: Any letter, A-Z, caps A-Z
or lowercase a-z
\w
: Any letter or number, *
: 0 or more timesRegexper:
Online Demo:
regexr.com
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With