Preg_match in php

I want to use preg_match() so that there are no special characters in this line, such as `` @ # $% ^ & / ''.

For instance:

Coding : Output is valid : the output is invalid (the line starts with a space)
Project management : output values ​​are valid (space between two words)
"Design23": output values ​​valid 23Designing : displayed incorrectly
123 : Invalid outputs

I tried, but could not reach a valid answer.

+4
source share
3 answers

Try

 '/^[a-zA-Z][\w ]+$/' 
0
source

Does this regex help?

^[a-zA-Z0-9]\w*$

It means:

  • ^ = this pattern should start at the beginning of the line
  • [a-zA-Z0-9] = this char can be any letter ( az and az ) or a digit ( 0-9 , also see \d )
  • \w = word character. This includes letters, numbers, and spaces (not new lines by default)
  • * = Repeat item 0 or more times
  • $ = this pattern should end at the end of the line

To fulfill the condition that I missed, try

^[a-zA-Z0-9]*\w*[a-zA-Z]+\w*$

The added extra material allows it to have a digit for the first character, but it should always contain a letter because of [a-zA-Z]+ , since + means 1 or more.

+2
source

If this is homework, you might just want to learn regular expressions:

0
source

Source: https://habr.com/ru/post/1311726/


All Articles