Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having trouble building a regular expression

Tags:

regex

I am trying to build a regular expression that has the following rules:

  • it must start with a character or an asterisk
  • a character can be followed by more characters, a full stop or the equals sign
  • a full stop must be followed by more characters or an asterisk
  • an asterisk must be followed by the equals sign

Legitimate cases would be:

  • *=text
  • word=text
  • word.word.word...word=text (ie. There can be many repititions of .word)
  • word.*=test

If this needs more clarification just let me know, it's kind of hard to explain it.

I currently have this for my regular expression:

(\w+\.)*(\*|\w+)\=\w+

Which correctly validates the given examples, however it matches ".*=INFO" which should be invalid. Anyone know how I can get it to not match this string?

like image 360
annedroiid Avatar asked Dec 10 '25 06:12

annedroiid


1 Answers

^(\*|\w+(\.\w+)*(\.\*)?)\=\w+$

This handles separately cases which start with an asterisk and those which start with a character, since I wasn't able to combine them.

^          begin
(
 \*        asterisk
|            or
 \w+       first word
 (\.\w+)*  optionally more words, separated by dot
 (\.\*)?   optional dot + asterisk
)
\=         equals
\w+
$          end
like image 139
Markus Laire Avatar answered Dec 13 '25 14:12

Markus Laire



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!