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.
你需要一个积极的先行断言 :
([A-Z]{1,3})(?==)
What you want is called a zero-width, lookahead assertion. You do:
()(?=)
In your case, this would be:
([A-Z^]{1,3})(?==)
The following will group everything before the "=" and everything after.
([^=]*)=([^=]*)
it reads something like this:
match any amount of characters thats not a "=", followed by a "=", then any amount of characters thats not a "=".
You can also put the equals sign in a non-capturing parans with (?: ... )
([ADM]{1,3})(?:=)
It's been a bit since I did this chapter of the book but I think that since you need both parts of the expression anyway, I did a split on the = resulting in myArray[0] == M, myArray[1] == A
I needed to match every character before the '=' so I came up with this
.*(?==)=
Matches every character before '=' but not "="
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.