Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex - Match a Pattern Before a Character

Tags:

c#

regex

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.

like image 317
bplus Avatar asked Jun 29 '09 21:06

bplus


People also ask

What does regex 0 * 1 * 0 * 1 * Mean?

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.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

How do you match everything before a character?

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.

How do I match a pattern in regex?

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


2 Answers

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})(?==)
like image 98
Conspicuous Compiler Avatar answered Oct 10 '22 04:10

Conspicuous Compiler


You need a positive lookahead assertion:

([A-Z]{1,3})(?==)
like image 23
RichieHindle Avatar answered Oct 10 '22 06:10

RichieHindle