i am trying to use a regular expression to match expression like code assignment for example:
"k=++g677"
"k=--j77"
"k=++gfrf677frfr"
i've tried:
([a-zA-Z])([a-zA-Z]|\d)*[=][+][+]|[-][-]|[*][*]|[\/][\/]([\d]*|([a-zA-Z]([a-zA-Z]\d)*))
but it's seemds to match also things like
"k++"
"k=++@ff"
can u help?
You may use
^[a-zA-Z_][\da-zA-Z_]*=([+*\/-])\1[a-zA-Z_][\da-zA-Z_]*$
Or, if you need to capture each detail into a separate group:
^([a-zA-Z_][\da-zA-Z_]*)(=)(([+*\/-])\4)([a-zA-Z_][\da-zA-Z_]*)$
See this regex demo and its graph:

Details
^ - start of string[a-zA-Z_] - an uppercase ASCII letter or underscore[a-zA-Z\d_]* - 0 or more alphanumerics / _ (replace with \w if it is not Unicode aware by default)= - an equal sign([+*\/-])\1 - +, *, / or - and then exactly 1 the same char[a-zA-Z_] - an ASCII letter or _[a-zA-Z\d_]* - 0 or more alphanumerics / _ (replace with \w if it is not Unicode aware by default)$ - end of string.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With