F
F
FilimoniC2013-01-20 15:31:42
Programming
FilimoniC, 2013-01-20 15:31:42

RegEx for words of letters _And_ numbers

Tell me, is it possible to check with one expression whether a word consists of letters and numbers. The conditions are as follows:
* Characters can be placed in any order (it can start / end with a letter or number)
* The word consists of Latin letters and numbers
* The word does NOT consist of letters only
* The word does NOT consist of numbers only

Answer the question

In order to leave comments, you need to log in

7 answer(s)
D
Dmitry Guketlev, 2013-01-20
@FilimoniC

Why not? Can. When writing regexps, you need to operate not with logical conditions, but with the order of characters in a string. Your string looks like this:
((one or more letters, then one or more digits) or (one or more digits, then one or more letters)) then optionally letters or digits
Here is the regression:
(([az]+\d+) |(\d+[az]+))[az\d]*
regexr.com?33fas

S
Sergey Cherepanov, 2013-01-20
@fear86

I got it like this:
(?<=\d)[A-z]|(?<=[A-z])\d

V
Vyacheslav Golovanov, 2013-01-20
@SLY_G

One is unlikely. And why? Do you need it to work, or do you need it to be cool? :)
You need three checks - that there are only letters + numbers, that there are letters, that there are numbers.

/^[a-z0-9]+$/ && /[a-z]/ && /[0-9]/

S
Sergey Cherepanov, 2013-01-20
@fear86

---

A
akzhan, 2013-01-20
@akzhan

/^(?:\d+\w|\w+\d)[\w\d]+$/

S
Sergey Cherepanov, 2013-01-23
@fear86

Fail regexr.com?33gal
oops should have come here habrahabr.ru/qa/32825/#answer_128961

A
aleksdenni, 2021-08-29
@aleksdenni

Perhaps so.
\b(\d+\w+)\b|\b(\w+\d+)\b|\b(\d+\w+\d+)\b|\b(\w+\d+\w+)\b

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question