我想#
\
\href
出现在s/(\\href\{.*?)#(.*?)\}/\1\\#\2/g
命令中。
通常情况下,我会编写一个正则表达式,例如gsub
,但我想\href
我们可以在这里首先提取#
内容,然后将\#
替换为Here is some text with a \href{./file.pdf#section.1.5}{link} to section 1.5.
gsub
。
RouterOutlet
一行中可以有多个链接。
问题
<a [routerLink]=['new']> New </a>
可以简化这些问题吗?
答案 0 :(得分:3)
您可以使用两个gsub:一个带有参数和一个块(用于href{...}
),一个带有2个参数(用#
替换\#
):
text = %q(Here is some text with a \href{./file.pdf#section.1.5}{link} to section 1.5.)
puts text.gsub(/href{[^}]+}/){ |href| href.gsub('#', '\#') }
#=> Here is some text with a \href{./file.pdf\#section.1.5}{link} to section 1.5.
如果您想从ruby -e
文件的test.txt
终端启动它,可以使用:
ruby -pe '$_.gsub(/href{[^}]+}/){ |href| href.gsub(%q|#|, %q|\#|) }' test.txt
# Here is some text with a \href{./file.pdf#section.1.5}{link} to section 1.5.
# Here is some text with a \href{./file.pdf#section.1.6}{link} to section 1.6.
# Here is some text with a \href{./file.pdf#section.1.7}{link} to section 1.7.
或
ruby -e 'puts ARGF.read.gsub(/href{[^}]+}/){ |href| href.gsub(%q|#|, %q|\#|) }' test.txt
# Here is some text with a \href{./file.pdf#section.1.5}{link} to section 1.5.
# Here is some text with a \href{./file.pdf#section.1.6}{link} to section 1.6.
# Here is some text with a \href{./file.pdf#section.1.7}{link} to section 1.7.
请勿混用ruby -pe
和ARGF.read
,它只会读取您文件的第一行!
答案 1 :(得分:3)
除非\href{..}
中包含的一个或多个网址在http://username:"sdkfj#lkn#"@domainname.org/path/file.ext
之类的引号之间包含密码部分,否则网址中{8}字符{}的唯一可能位置是结束并分隔片段部分:#
。
换句话说,如果我没有错,那么./path/path/file.rb?val=toto#thefragmentpart
每#
最多可以逃脱href{...}
。然后你就可以这样做:
text.gsub(/\\href{[^#}]*\K#/, "\\#")
字符类[^#}]
禁止使用字符}
,并确保您始终处于大括号之间。