preg_match (...) for various types of text

3

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'

    
asked by anonymous 29.08.2016 / 11:48

2 answers

4

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.

  • 123teste
  • teste123
  • test123teste
  • 123teste123
  • t3st3

Explanation

  • ([[: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.
29.08.2016 / 15:35
2

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+)?)

[a-zA-Z] +

House with letters from a to z or from A to Z with 1 or more occurrences.

(\ d +

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.

\ d +

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.

    
29.08.2016 / 14:03