gowtham g
gowtham g

Reputation: 43

Regular expression for text followed by comma

Hi I tried writing regular expression for below pattern

D123456789,D123456789,D123456789,D123456789,D123456789,D123456789,D123456789

The pattern should accept the first alphabet as D followed by 9 digits with a comma. The last comma is not recommended.

My pattern is -

(^[Dd][0-9]{9}[,])+

Please guide me on how to do this?

Upvotes: 0

Views: 567

Answers (1)

Valdi_Bo
Valdi_Bo

Reputation: 30971

I assume that you want to match each D or d followed by exactly 9 digits (no more), so it is not enough to write /D\d{9}/gi, as it would have matched this D and initial 9 digits from a longer digit string.

The proper regex should contain also a positive lookahead, including either a comma or the end of string. Something like:

/D\d{9}(?=,|$)/gi

I assume that you are not "interested" in the , after the digits, so I didn't include it in the match.

Note also that your source sample contains 10-digit strings (not 9). Did you make a mistake in this detail?

Upvotes: 1

Related Questions