0

I have a case like this:

<p class="definitione **form**">Data una grandezza indica la variazione.</p><br/>
<p class="**form**">Due casi molto comuni sono:</p><br/>
<ul><br/>
&nbsp;&nbsp;&nbsp;&nbsp;<li class="**form** placeholder">è il valore iniziale della grandezza.</li><br/>
&nbsp;&nbsp;&nbsp;&nbsp;<li class="definition **form** placeholder">è il valore di in un secondo punto.</li><br/>
</ul><br/>
<p id="form">Se la grandezza aumenta, la variazione è positiva</p><br/><br/>

I need to find (and replace) only form word (and not the other word) into attribute class.

Try with this regex:

(?<=class=")form(?=")

select only:

<p class="definitione form">Data una grandezza indica la variazione.</p><br/>
<p class="**form**">Due casi molto comuni sono:</p><br/>
<ul><br/>
&nbsp;&nbsp;&nbsp;&nbsp;<li class="form placeholder">è il valore iniziale della grandezza.</li><br/>
&nbsp;&nbsp;&nbsp;&nbsp;<li class="definition form placeholder">è il valore di in un secondo punto.</li><br/>
</ul><br/>
<p id="form">Se la grandezza aumenta, la variazione è positiva</p><br/><br/>

But I need to select ALL form in all class attribute

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
steguozzo
  • 55
  • 9
  • 1
    While your specific problem might be solvable with a regex, be aware of this: [You can't parse HTML with regex](http://stackoverflow.com/a/1732454/256544) – splash Sep 19 '13 at 10:58
  • The question doesn't make sense, well, it's not really a question - more a statement. Are you trying to select elements with a class of `form` when that is the only class? (so `form spec` wouldn't match) – MDEV Sep 19 '13 at 11:14
  • Hi SmokeyPHP, sorry for grammar and error, I change the body of statement! I need to find only the word FORM into quotation mark. – steguozzo Sep 19 '13 at 11:56
  • Bill the Lizard, thank you so much! Apologies! – steguozzo Sep 24 '13 at 12:00

1 Answers1

1

To match classes with form in them:

(class="[^"]*)(form)([^"]*")

See a live demo of this regex, which uses look arounds to assert what precedes and follows the target.

Note that group 2 matches "form", not the whole expression. This is because variable length look behinds are illegal. Your replacement should refer to group 1 and 3 to put them back.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Yes, but I need to find **form** when there is another word too, find ALL **form** into ALL class attribute, independently if it's "alone"... – steguozzo Sep 19 '13 at 15:04
  • Follow the link to the live demo and tell me what examples it fails to work correctly with. Add more examples if you like and post the new link as a comment here. – Bohemian Sep 19 '13 at 15:32
  • 1
    I've edited the answer to match form anywhere in the class. Group 2 is your match – Bohemian Sep 20 '13 at 09:11