使用正则表达式查找行开始 [英] Finding line beginning using regular expression

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

问题描述

我想从所有的 jQuery完成"中删除一个 4000 行的 HTML 文件.div 中的属性.

I want to strip a 4000-line HTML file from all the jQuery "done" attributes in a div.

<DIV class=menu done27="1" done26="0"
done9="1" done8="0" done7="1"
done6="0" done4="20">

应替换为:

<DIV class=menu>

这个实验中,我可以用这个正则表达式来做:

In this experiment I can do it with this regular expression:

[ ^]done[0-9]+="[0-9]+"

使用 Notepad++ 5.6.8 Unicode,文件以 ANSI 编码,我将此正则表达式放在查找内容"中场地.它只替换以空格开头的 5 次出现,它将错过从行首开始的 2 次出现.

Using Notepad++ 5.6.8 Unicode, with a file encoded in ANSI, I'm putting this regex in the "Find what" field. It only replaces the 5 occurrences starting with a space, it will miss the 2 occurrences starting at the beginning of a line.

如何构造一个正则表达式来删除以关键字开头的 HTML 元素的所有属性?

How can I construct a regex to remove all the attributes of an HTML element starting with a keyword?

推荐答案

扩展将\n"替换为LINEBREAK"

非常感谢大家及时回复.按照你的建议,我做了以下事情:

Thanks a lot to all for these timely replies. Following your advices, here's what I did:

  • Notepad++ > View > Show Symbol > Show End Of Line" 在每一行末尾显示CR+LF".
  • "Notepad++ > Search > Find", "Search mode" = "Normal", 确保 "Find what" = "LINEBREAK" 什么也没找到
  • "Search mode" = "Extended", "Find what" = "\n\r" 只找到双换行符(CR + LF + 一个空行);"\n \r" 一无所获;然而 "\n" 确实能找到所有的换行符,而且只有它们.
  • 我的Towncar.htm"测试文件保存为Towncar_02.htm"(也以ANSI编码)
  • 在扩展"下,将所有\n"替换为LINEBREAK"(注意尾随空格)
  • 在正则表达式"下,替换每个出现的:

  • "Notepad++ > View > Show Symbol > Show End Of Line" shows "CR+LF" at each line end.
  • "Notepad++ > Search > Find", "Search mode" = "Normal", made sure that "Find what" = "LINEBREAK" finds nothing
  • "Search mode" = "Extended", "Find what" = "\n\r" only finds the double-breaks (CR + LF + a blank line); "\n \r" find nothing; yet "\n" does find exactly all line breaks, and only them.
  • Saving my "Towncar.htm" test file as "Towncar_02.htm" (also encoded in ANSI)
  • Under "Extended", replaced all "\n" with "LINEBREAK " (notice the trailing space)
  • Under "Regular expression", replaced each occurrence of:

 done[0-9]*="[0-9]*"

(在完成"之前要小心检查标题空间
并且没有尾随空间!见下文)

带有空字符串

  • 在扩展"下,将每次出现的LINEBREAK"替换为\n"(这次在LINEBREAK"之后没有尾随空格!)
  • 检查生成的Towncar.htm"文件(经过几次外观重新格式化后)看起来不错且漂亮,并且在刷新后,它仍然呈现与Towncar_02.htm"备份相同的效果.

回忆和笔记:

  • 这个论坛显然在 Chrome 4 中运行良好;但是对于某些浏览器(例如 IE6 和其他已停产的浏览器),在某些情况下会导致一些伪影;所以,要小心:
  • 即使论坛没有在您的浏览器中显示它,也有一个标题空间,即在正则表达式的开头(上面的done..."正则表达式)和里面,所以用起始空格替换以done"开头的字符串,从而更确定不要用撤消"或美沙酮"或其他方式改变最终的其他字符串
  • 同样,即使论坛在您的浏览器中显示了一个,Regex 末尾也没有尾随空格
  • 在正则表达式中,[0-9] 匹配 1 次且仅出现 1 次的任何十进制数字(0-9 范围内的字符);IOW 它匹配 « 0 » 或 « 1 » 或 « 9 » 等,但不匹配 « 01 » 或 « 835 » 或 « »(空字符串)或任何一个.
  • *(星号)匹配前一个字符的 0 次或多次(这里它匹配空字符串或任何专门由数字组成的字符串)
  • 同样,+(加号)匹配前一个字符的 1 次或多次(这里它匹配任何字符串,至少 1 个字符长,仅由数字组成)
    参考:http://sourceforge.net/apps/mediawiki/notepad-plus/index.php?title=Regular_Expressions#Notepad.2B.2B_regex_syntax
  • This forum apparently works well in Chrome 4; but with some browsers (e.g. IE6 and other discontinued ones), under some circumstances, it causes some artifacts; so, be careful:
  • even if the forum doesn't show it in your browser, there is a heading space, i.e. at the beginning of the Regex (the " done..." Regular expression above) and inside it, so to replace only strings starting with " done", with the starting space, thus making even surer to NOT alter eventual other strings with "undone" or "methadone" or else
  • same way, even if the forum shows one in your browser, there is no trailing space at the end of the Regex!
  • in the Regex, [0-9] matches 1 and only 1 occurrence of any decimal digit (characters in the 0-9 range); IOW it matches « 0 » or « 1 » or « 9 » etc, but NOT « 01 » or « 835 » or « » (the empty string) or whichever.
  • * (asterisk) matches 0 or more times the previous character (here it matches the empty string or any string made exclusively of digits)
  • samewise, + (plus sign) matches 1 or more times the previous character (here it matches any string, at least 1 character long, made exclusively of digits)
    Ref: http://sourceforge.net/apps/mediawiki/notepad-plus/index.php?title=Regular_Expressions#Notepad.2B.2B_regex_syntax

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

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