简体   繁体   English

仅在某些字符串中替换字符

[英]Replace character only in certain string

I need to replace every "_" with "-" in html file but only in tag and only in "name" attribute. 我需要在html文件中用“-”替换每个“ _”,但只能在标记中,并且仅在“名称”属性中。

So every this: 所以每一个:

<a name="menu_portlet_test"> or <a name="whatever_is_here">

should become this: 应该变成这个:

<a name="menu-portlet-test"> and <a name="whatever-is-here">

Can't figure out how to force something like sed/awk to do it. 无法弄清楚如何强制执行sed / awk之类的操作。 Help! 救命!

sed ':a
s/\(<[^>]* name="[^"]*\)_\([^"]*"\)/\1-\2/g;ta' YourFile

Should do most of you job. 应该做你们大多数人的工作。 Not perfect due top html possibilities but should be 99,9% ok 由于顶部html的可能性不理想,但应该99.9%可以

explaination 解释

s//g

  • Search the pattern ( < followed by any non > ([^>] ) followed by name=" followed by (any non " ( [^"] ) ) [ as group 1] followed by [so first between quote after name=] followed by ( any non " ( [^"]* ) followed by "`) [ as group 2 ] 搜索模式( <后跟任何非> ([^>] ) followed by name =“, followed by (any non( [^“] ) ) [ as group 1] followed by [so first between quote after name=] followed by ( any non [so first between quote after name=] followed by ( any non( [^”] * ) followed by “`)[作为组2]
  • Replace it by content of group 1 followed by - followed by content of group 2 将其替换为组1的内容,然后是-然后是组2的内容
  • g do it for any occurence on the line. g进行任何在线处理。 This change 1 _ per name="" but on any name= of the line. 每行name =“”更改1 _,但在该行的任何name =上。 <... name="bla_bla_bla"> ... <... name="other_bla_bla"> ... change to <... name="bla-bla_bla"> ... <... name="other-bla_bla"> ... <... name="bla_bla_bla"> ... <... name="other_bla_bla"> ...更改为<... name="bla-bla_bla"> ... <... name="other-bla_bla"> ...

ta

  • if a change occur in previous s// , redo the same action with modified content( in fact it is a if/goto to label :a ) 如果先前的s//发生了更改,请对修改后的内容重做相同的操作(实际上,它是对if:go的标签:a

Use a proper HTML handling tool, for example xsh , a wrapper around Perl's XML::LibXML . 使用适当的HTML处理工具,例如xsh ,它是Perl的XML :: LibXML的包装器。 The following commands can be saved in a script, or entered from its interactive environment: 以下命令可以保存在脚本中,也可以从其交互式环境中输入:

open :F html file.html ;
for //@name set . xsh:subst(., '_', '-', 'g') ;
save :b ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅在字符串的开头替换字符 - Replace a character only in the start of a string Javascript 检查字符串是否仅包含某个字符 - Javascript check if string contains only certain character 只允许字符串中的某个字符。使用Javascript - Allow only certain character in string. Javascript 仅在字符串后跟某些字符时替换字符串中的空格 - Replace space in string only if followed by certain characters Bash从字符串中剪切第一个和/或最后一个字符,但前提是它是某个特定字符 - Bash shave a first and/or last character from string, but only if it is a certain character 仅在某些字符分隔的文本的某些部分中替换子字符串 - Replace substring only in certain portions of text delimited by some character 仅当单词前面没有特定字符时才替换它 - Replace a word only if it's not preceded by a certain character(s) 在给定的字符串中用 --[character]-- 替换某些字符的出现的函数 - Function to replace occurrence of certain characters with --[character]-- in a given string 替换字符串末尾的字符,直到到达某个字符正则表达式 - Replace characters at end of string, until it reaches a certain character regex PHP在HTML标记中找到某些字符并用字符串替换整个标记 - PHP find certain character in HTML tag and replace the whole tag by string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM