DJafari
DJafari

Reputation: 13545

regular expression to match string

i want a pattern to match below strings :

count="2"
count = "2"
count   =    "2"
count='2'
count = '2'
count  =  '2'
count=2
count = 2
count   =   2

Upvotes: 0

Views: 174

Answers (1)

Kobi
Kobi

Reputation: 138137

It isn't too clear what should the pattern not match, but you may be looking for something like this:

count\s*=\s*(["']?)\d+\1

That regex will allow matching quotes (or no quotes) around a number. \1 matches the same thing the first captured group (["']?) matched before: ", ', or nothing, so it wouldn't allow mixed quotes. (?:"\d+"|'\d+'|\d+) would have worked similarly.

You may want a better definition of strings or numbers, for example:

count\s*=\s*(?:"(?:[^"\n\r\\]|\\.)*"|'(?:[^'\n\r\\]|\\.)*'|\d+)

The regex allows strings with escaped characters an no new-lines, or integer literals.

Upvotes: 4

Related Questions