Home > Software engineering >  Can't generate a regular expression
Can't generate a regular expression

Time:12-09

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 : 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] ...

CodePudding user response:

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 $.

  • Related