用正则表达式匹配换行符 [英] Match line break with regular expression

查看:78
本文介绍了用正则表达式匹配换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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.

我想使用正则表达式在 Permits 的末尾插入 </a>.碰巧的是,我所有类似的 html/text 块中都已经有一个换行符.我相信我需要找到一个换行符 \n,其中该行包含(或以)<li><a href="#">.

I want to use a regular expression to insert </a> at the end of Permits. It just so happens that all of my similiar 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="#">.

推荐答案

您可以搜索:

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

并替换为:

$0</a>

其中 $0 是整个匹配项.确切的语义将取决于您使用的语言.

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

警告:您应该避免使用正则表达式解析 HTML.原因如下.

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

这篇关于用正则表达式匹配换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆