如何在TextWrangler中的两个分隔符之间替换项目

Nia*_*yle 2 regex textwrangler grep

我想用这样的拼音转换斜线替换一个拼音符号:

/anycharacter*ou*anycharacter/
Run Code Online (Sandbox Code Playgroud)

/anycharacter*au*anycharacter/
Run Code Online (Sandbox Code Playgroud)

我的意思是我想在所有情况下在任何两个语音斜线之间用"au"替换"ou".例如:

<font size=+2 color=#E66C2C> jocose /d??'kous/</font>
    =  suj vour ver / suwj dduaf 
Run Code Online (Sandbox Code Playgroud)

<font size=+2 color=#E66C2C> jocose /d??'kaus/</font>
    =  suj vour ver / suwj dduaf  
Run Code Online (Sandbox Code Playgroud)
  • 文本文件包含HTML代码和一些正斜杠(如A/B而不是A或B)
  • 字符串"anycharacter"可以是任何字符,一个或多个字符或无字符.例如:/ folou /,/ houl /,/ sou /,/dʒə'kousnis/ ...

到目前为止,我一直在使用:

Find: \/(.*?)\bou*\b(.*?)\/\s
Replace: /\1au\2\3\4/ 
Run Code Online (Sandbox Code Playgroud)

但是它会找到任何/.../之间的所有字符串,包括正常的正斜杠和HTLM斜线,当替换它时会绕过诸如/ gou /,/ tou /等项目.与上面的例子一样,输出是:

<font size=+2 color=#E66C2C> jocose /d??'kaus/</font>
    =  suj vaur ver / suwj dduaf 
Run Code Online (Sandbox Code Playgroud)

注意:正常斜线之前的"vour"被"vaur"取代不是我的目的.

你能指导我如何解决上述问题吗?非常感谢.

Cod*_*key 7

可满足您需求的最简单匹配表达式(符合POSIX ERE)是:

(/[^ \t/<>]*?)ou([^ \t/<>]*?/)
Run Code Online (Sandbox Code Playgroud)

细分,这意味着:

(             # Capture the following into back-reference #1
  /           #   match a literal '/'
  [^ \t<>]    #   match any character that is not a space, tab, slash, or angle bracket...
    *?        #     ...any number of times (even zero times), being reluctant
)             # end capture
ou            # match the letters 'ou'
(             # Capture the following into back-reference #2
  [^ \t/<>]   #   match any character that is not a space, tab, slash, or angle bracket...
    *?        #     ...any number of times (even zero times), being reluctant
  /           #   match a literal '/'
)             # end capture
Run Code Online (Sandbox Code Playgroud)

然后使用replace表达式 \1au\2

/如果在它们之间有空格,制表符,尖括号(<>)或另一个正斜杠(/),这将忽略字符之间的文本.如果您知道的其他字符不会出现在其中一个表达式中,请将其添加到字符类([]组)中

在我的模拟器中,它会变成这个文本:

<font size=+2 color=#E66C2C> jocose /d??'kous/</font>
    =  suj vour ver / suwj dduaf. 
Either A/B or B/C might happen, but <b>at any time</b> C/D might also occur
Run Code Online (Sandbox Code Playgroud)

......进入本文:

<font size=+2 color=#E66C2C> jocose /d??'kaus/</font>
    =  suj vour ver / suwj dduaf. 
Either A/B or B/C might happen, but <b>at any time</b> C/D might also occur
Run Code Online (Sandbox Code Playgroud)

只要问一下你有什么不明白的地方!如果您愿意,我还可以解释您之前尝试使用的问题.

编辑:

上述表达式匹配整个语音转录集,并使用匹配的某些部分替换其他部分完全替换它.下一次比赛尝试将在当前比赛后开始.

因此,如果ou/分隔的语音表达式中可能出现多次,则需要多次运行上述正则表达式.对于一次性执行,语言或工具需要支持可变长度前瞻和后视(集体环视)

据我所知,这只是微软的.Net Regex和正则表达式的JGSoft"风格"(在EditPad Pro和RegexBuddy等工具中).POSIX(UNIX grep需要)不支持任何类型的环视和Python(我认为 TextWrangler使用)不支持可变长度环视.我相信如果没有可变长度的环视,这是不可能的.

需要可变长度环视的表达式,并根据需要执行以下操作:

(?<=/[^ \t/<>]*?)ou(?=[^ \t/<>]*?/)
Run Code Online (Sandbox Code Playgroud)

...并且还需要修改替换表达式,因为您只匹配(并因此替换)要替换的字符:

au
Run Code Online (Sandbox Code Playgroud)

它的工作方式大致相同,只是它只匹配ou,然后运行一个检查(称为零宽度断言)以确保它前面有一个/和任意数量的某些字符,并且后面跟着任意数量的某些字符那么一个/.