Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect strings containing only digits, letters and one or more question marks

I am writing a python regex that matches only string that consists of letters, digits and one or more question marks.

For example, regex1: ^[A-Za-z0-9?]+$ returns strings with or without ?

I want a regex2 that matches expressions such as ABC123?A, 1AB?CA?, ?2ABCD, ???, 123? but not ABC123, ABC.?1D1, ABC(a)?1d on mysql, I did that and it works:

select *
from (
select * from norm_prod.skill_patterns
where pattern REGEXP '^[A-Za-z0-9?]+$') AS XXX
where XXX.pattern not REGEXP '^[A-Za-z0-9]+$'
like image 473
LearnToGrow Avatar asked Sep 03 '25 03:09

LearnToGrow


1 Answers

How about something like this:

^(?=.*\?)[a-zA-Z0-9\?]+$

As you can see here at regex101.com

Explanation

The (?=.*\?) is a positive lookahead that tells the regex that the start of the match should be followed by 0 or more characters and then a ? - i.e., there should be a ? somewhere in the match.
The [a-zA-Z0-9\?]+ matches one-or-more occurrences of the characters given in the character class i.e. a-z, A-Z and digits from 0-9, and the question mark ?.

Altogether, the regex first checks if there is a question mark somewhere in the string to be matched. If yes, then it matches the characters mentioned above. If either the ? is not present, or there is some foreign character, then the string is not matched.

like image 191
Robo Mop Avatar answered Sep 04 '25 23:09

Robo Mop