-1

I want to match these

<div class="name">a</div>
<div class="name">b</div>
<div class="name">c</div>

so would this be enough : <div class="name">(.*)<\/div> ??

or <div class="name">(.*?)<\/div> ?

and why this [<div class="name">](.*)[/div] doesn't matches?

  • `[]` in regex called [character class](http://www.regular-expressions.info/charclass.html). It would match a single character from the list. – Avinash Raj Oct 06 '14 at 10:07
  • @AvinashRaj I know that but sorry your comment doesn't answer to my doubts Sir. – user3522906 Oct 06 '14 at 10:08
  • 2
    I don't know what your doubt is? Explain your question clearly. – Avinash Raj Oct 06 '14 at 10:09
  • `[]` means one charcter out of the characters inside.You need to add `+` or `*` so it mactches all of the characters defined inside.But their order can be random.`[/div]` can match `/vid` as well and `v` as well – vks Oct 06 '14 at 10:10
  • `?` after the `*` or `+` would act like a reluctant quantifier. By default `*` and `+` are [greedy](http://www.regular-expressions.info/repeat.html). You need to add `?` after those symbols to force the regex engine to do a non-greedy match(shortest possible match). But `?` after a character or character class or capturing or non-capturing group or escape character would make the previous token or char as optional. – Avinash Raj Oct 06 '14 at 10:14

1 Answers1

0
<div class="name">.*?</div>

will probably work best. Using the greedy version <div class="name">.*</div> would match multiple <div>s as one, e.g. <div class="name">foo</div><div>bar</div> would match as a whole.


[<div class="name">](.*)[/div]

does not work because the square braces [] denote a character class. So what the pattern does is this:

[<div class="name">] // match one of the characters `<div clas="nme>` literally
(.*) // match any character except newline, any number of times
[/div] // match one of the characters `/div` literally

P.S.: Note that <div class="name">.*?</div> won't work with nested <div>s. E.g. in

<div class="name">
    <div>foo</div>
</div>

it would only match

<div class="name">
    <div>foo</div>

because the pattern only consumes the string up to the first occurence of </div>.

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149