Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match a word with at least one letter and any number of digits (no lookaround) [duplicate]

Tags:

regex

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?

like image 902
AnduA Avatar asked Nov 27 '15 23:11

AnduA


People also ask

What does ?= Mean in regex?

?= 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).

How do you match letters in regex?

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 "@" .


1 Answers

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 times

Regexper:

Regexper

Online Demo:

regexr.com

like image 122
Ben Aubin Avatar answered Oct 14 '22 18:10

Ben Aubin