Search code examples
phpregexregular-language

Can't generate a regular expression


I'm trying to generate a regular expression to detect any string that :

  • Starts with the [
  • Ends with ]
  • Contains any word or number or space

Example : [Maxsens8 Innovations]

I'm using : https://regex101.com

I create this regular expression : ^\[[a-zA-Z0-9\]]$

enter image description here

enter image description here

This regular expression does not match to expressions that i'm looking for and i thing the problem

is about the last character .

I will be gratefull if someone explain to me how to generate the right regular expression that

detect strings like : [Maxsens Innovations] [Verifik8] [Divoluci] ...


Solution

  • Try:

    ^\[[a-zA-Z0-9 ]*\]$ or ^\[[a-zA-Z0-9\s]*\]$

    • \]$ Match ] followed by end of line ($)
    • [ and ] are meta-characters hence should be escaped.
    • Use \s to match any white-space char (including newline) or single space.

    Note: This shall match against single input line. If you have multiple fragments per line then skip the line start and end markers viz., ^ and $.