0

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
royhowie
  • 11,075
  • 14
  • 50
  • 67
DJafari
  • 12,955
  • 8
  • 43
  • 65
  • As I've mentioned, you could explain what *isn't* matched by the regex, and what language are you using - you'd definitely get better answers that way `:)` – Kobi Apr 30 '11 at 13:28
  • i want get html attribute in php, have better idea ? :) – DJafari Apr 30 '11 at 13:34
  • Actually, yes, there are much better ideas. Using an [html parser](http://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php) may be much easier than a regex, and get better results. – Kobi Apr 30 '11 at 13:36
  • because i want it for my template engine, i think regex is better than HTMLParser :-? – DJafari Apr 30 '11 at 13:40

1 Answers1

4

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.

Kobi
  • 135,331
  • 41
  • 252
  • 292
  • Your answer would be even better if you'd explain the grouping & back-reference :) – Bart Kiers Apr 30 '11 at 13:23
  • Thank you, i use from this : `count\s*=\s*(["\']?)([a-z0-9]+)\1` and work perfect for me, of course with your help ! ;) – DJafari Apr 30 '11 at 13:26
  • +1 Good answer. I would just add that the efficiency (speed) of: `"(?:[^"\n\r\\]|\\.)*"` can be improved by _"[Unrolling-the-Loop](http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124 "from: Mastering Regular Expressions (3rd Edition)")"_ like so: `"[^"\n\r\\]*(?:\\.[^"\n\r\\]*)*"`. The same enhancement can be applied to the single quoted expression. Note that this speed enhancement only applies to backtracking NFA engines - i.e. Javascript, Ruby, PHP, Perl, Python, .NET etc. – ridgerunner Apr 30 '11 at 15:55