How can I validate the texts below?
Letters (including accents) and numbers (without spacing), separated by. or,. ex: 'hello'
Whole numbers and / or tenths. ex: '22' or '2.2'
How can I validate the texts below?
Letters (including accents) and numbers (without spacing), separated by. or,. ex: 'hello'
Whole numbers and / or tenths. ex: '22' or '2.2'
In a other question I have a # that can be of help.
In your case it is a bit vague, since you do not specify if the numerals can come before, after, be in the middle, if it can be mixed.
~^(?=.*[[:alpha:]].*)([[:alpha:]]*(\d+([.,]\d+)?)?[[:alpha:]]*)*$~u
This REGEX will accept all the questions I've put above.
([[:alpha:]]*(\d+([.,]\d+)?)?[[:alpha:]]*)*
- This part frees to have letters before after the digits, but note that they are all optional, which would release NADA
. (?=.*[[:alpha:]].*)
- This part ensures that string
has at least one alpha. As I said in another answer.
[:alpha:]
= [a-zA-Z]
, but note that there is a difference, as I will use the u
modifier of unicode, the ideal is to use [:alpha:]
as it will also include accented characters. Nothing that a good studied about regular expressions does not solve.
Only the part of the accents that does not include, but the remainder is ok.
[a-zA-Z]+(\d+((\.|,)\d+)?)
House with letters from a to z or from A to Z with 1 or more occurrences.
House with 1 or more numerical digits and start a group.
After the digits must have a. (dot) or a, (comma), the parentheses are to group a segment.
After the dot or comma, another sequence of digits
Closes the 2 groups that have been opened before, the most internal being optional.
It's kind of complicated to describe regex by text, I find it hard to understand, especially in the beginning.
This site regexpal serves to test regex, makes life a lot easier.