0

I want a regular expression (to search in phpstorm) that will search for the starting tag and it's corresponding closing tag in all html files of my project directory.

E.g here is my code

<ul class="sub-menu">
<li id="menu-item-215" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-215"><a href="news-updates/index.html"><span>News / Updates</span></a></li>
<li id="menu-item-295" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-295"><a href="offers/index.html"><span>Offers</span></a></li>
</ul>

I want phpstorm to search for exact these lines of code starting from start ul tag and closing ul tag. I then want to replace these lines with a space. I just want to delete this subheading tag and its content from all my html files at once. How do I do it?

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
Faizan
  • 1,847
  • 8
  • 40
  • 63
  • **Don't use regular expressions to parse HTML. Use a proper HTML parsing module.** You cannot reliably parse HTML with regular expressions, and you will face sorrow and frustration down the road. As soon as the HTML changes from your expectations, your code will be broken. See http://htmlparsing.com/php or [this SO thread](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) for examples of how to properly parse HTML with PHP modules that have already been written, tested and debugged. – Andy Lester Feb 12 '15 at 13:46
  • @AndyLester Thanks but I just want to replace the entire tag and delete it in my all my project files. Can't I simply do it using PHPStorm editor search and replace? – Faizan Feb 12 '15 at 13:49
  • 1
    I didn't realize PHPStorm is an IDE. Then I'm not sure what I'd suggest. The problem with parsing HTML with regexes is that regexes are not up to the task of handling arbitrary HTML, and I don't know if PHPStorm will handle multiline regexes. See http://htmlparsing.com/regexes for examples of valid HTML that may stymie your regexes. – Andy Lester Feb 12 '15 at 14:17

2 Answers2

1

Okay so I finally did it using this regex in PhpStorm search and replace:

<ul class="sub-menu">(.*\n.*.\n.*)</li>\n</ul>
Faizan
  • 1,847
  • 8
  • 40
  • 63
0

Try this (big?) regex:

<ul\s+class="sub-menu">\s+<li\s+id="menu-item-215"\s+class="menu-item\s+menu-item-type-post_type\s+menu-item-object-page\s+menu-item-215"><a\s+href="news-updates/index\.html"><span>News\s+/\s+Updates</span></a></li>\s+<li\s+id="menu-item-295"\s+class="menu-item\s+menu-item-type-custom\s+menu-item-object-custom\s+menu-item-295"><a\s+href="offers/index\.html"><span>Offers</span></a></li>\s+</ul>

It will match exactly the text in your post.

HTH

Stephan
  • 41,764
  • 65
  • 238
  • 329