I'm currently building a toy assembler in c# (going through The Elements Of Computing Systems book).
I need to match a very simple pattern, I thought this would be a good time to learn some regex but I'm struggling!
In the following examples I'd just like to match the letters before the '='
M=A
D=M
MD=A
A=D
AD=M
AMD=A
I've come up with the following:
([A-Z]{1,3})=
However this also matches the '=' which I don't want.
I also tried:
([A-Z^\=]{1,3})=
But I still have the same problem - it a matches the '=' sign as well.
I'm using this site to test my regexes.
Any help would be really appreciated. Thank you in advance.
Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.
$ means "Match the end of the string" (the position after the last character in the string).
A regular expression to match everything before a specific character makes use of a wildcard character and a capture group to store the matched value. Another method involves using a negated character class combined with an anchor. Let's investigate various methods of writing this.
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).
What you want is called a zero-width, lookahead assertion. You do:
(Match this and capture)(?=before this)
In your case, this would be:
([A-Z^]{1,3})(?==)
You need a positive lookahead assertion:
([A-Z]{1,3})(?==)
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