views:

24

answers:

3

Hello,

i have a text and want to find all occurences of: prefix RM-EC + 3 digit number. For example RM-EC001, RM-EC099 or RM-EC100. But RM-EC99 should not be found.

Thank you.

+4  A: 

Try this:

RM\-EC\d{3}

As you're using SQL Server, I just found you can't to use quantifiers:

CREATE TABLE #test (value varchar(10))
INSERT INTO #test VALUES ('RM-EC001')
INSERT INTO #test VALUES ('RM-EC099')
INSERT INTO #test VALUES ('RM-EC100')
INSERT INTO #test VALUES ('RM-EC99' )
SELECT * FROM #test WHERE value LIKE 'RM-EC[0-9][0-9][0-9]'
Rubens Farias
You might need leading and trailing wild cards as well `'%RM-EC[0-9][0-9][0-9]%'` unless it has to match exactly.
Martin Smith
Nice point @Martin
Rubens Farias
+1  A: 

The regular expression should look like this:

RM-EC[0-9]{3}

Depending on the regular expression implementation, you could also use \d instead of [0-9].

Gumbo
+1  A: 

I know you asked for a RegEx, but since you also tagged the question as SQL Server, I'll point out that this would also work:

WHERE YourColumn LIKE 'RM-EC[0-9][0-9][0-9]'
Joe Stefanelli