39

The text:

<li><a href="#">Animal and Plant Health Inspection Service Permits
    Provides information on the various permits that the Animal and Plant Health Inspection Service issues as well as online access for acquiring those permits.

I want to use a regular expression to insert </a> at the end of Permits. It just so happens that all of my similar blocks of HTML/text already have a line break in them. I believe I need to find a line break \n where the line contains (or starts with) <li><a href="#">.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
The Muffin Man
  • 19,585
  • 30
  • 119
  • 191
  • I have a ton of Photoshop templates with text given to me and I have to turn them into html. Click - control V is getting old when putting html in the text. I'm using dreamweaver which has a regular expression feature built into the search. – The Muffin Man Mar 04 '11 at 23:26

2 Answers2

40

You could search for:

<li><a href="#">[^\n]+

And replace with:

$0</a>

Where $0 is the whole match. The exact semantics will depend on the language are you using though.


WARNING: You should avoid parsing HTML with regex. Here's why.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
NullUserException
  • 83,810
  • 28
  • 209
  • 234
  • 1
    What is if the line break is `\r` not `\n`? i.e is there any global or general symbol for lines breaks? – SaidbakR Jun 05 '15 at 15:19
  • 1
    and what if it's either \n or \r\n and you don't know which it will be? – ABCD.ca Aug 19 '16 at 18:25
  • 9
    @ABCD.ca `[^\r\n]+` handles that. That's a negated character class that matches any character that's not `\r` or `\n` (ie: `[^\r\n]+` is exactly equivalent to `[^\n\r]+`) – NullUserException Aug 19 '16 at 21:45
2

By default . (any character) does not match newline characters.

This means you can simply match zero or more of any character then append the end tag.

Find: <li><a href="#">.* Replace: $0</a>

mickmackusa
  • 43,625
  • 12
  • 83
  • 136