Thalecress
Thalecress

Reputation: 3451

Regular expression for 4 or 5 digits in a row followed by a letter in a longer string?

For a text like

1" 77568T86 34 
2" 5347A1Q 456

I'd like to return strings 77568Tand 5437A

I'm guessing I want something that begins like \d{4,5}

EDIT: Thanks for all the responses. Unfortunately, nothing is working in notepad++, even though they work with online regex testers.

I think the problem is notepad++'s handling of {} because (\d[A-Z]) finds matches.

Advice?

Upvotes: 3

Views: 8173

Answers (4)

burning_LEGION
burning_LEGION

Reputation: 13450

use this regex (?i)\b\d{4,5}[a-z]

Upvotes: 0

zb226
zb226

Reputation: 10500

This should work:

\d{4,5}[A-Z]

See for yourself

Upvotes: 1

Spudley
Spudley

Reputation: 168655

You got the number bit right; you just need to add a letter class to the end of your expression:

\d{4,5}[a-zA-Z]

(this allows upper or lower case; remove the a-z if you only want upper case)

Upvotes: 1

Michael Berkowski
Michael Berkowski

Reputation: 270609

Yes you're on the right track. Just add a single [A-Z] following the number group (use [A-Za-z] if it should be case insensitive).

\d{4,5}[A-Z]

If it should be preceded by whitespace or some boundary, prepend a \b

\b\d{4,5}[A-Z]

I'm not familiar with how Notepad++ handles match capture groups, but it is likely you will want the whole thing surrounded in ()

\b(\d{4,5}[A-Z])

Upvotes: 4

Related Questions