2016-09-10 9 views
0

私は二重引用符のすべての出現箇所を一重引用符に置き換えようとしています。例:SublimeText regexpルビタグ内の引用符を置き換えます

  • <%= t(".headline") %>
  • <%= f.text_field :email, class: "center big" %>

私は次のようにグループ内t(".headline")と一致する(?<=<%=\s)(.*)(\")(.*)(?=\s%>)試してみた:もちろん、それはすべてのoccurances、およびグループのすべてを捕捉しない

1. [4-16] `t(".headline` 
2. [16-17] `"` 
3. [17-18] `)` 

をelse。

また、私はサブライムのReplace: [input box]で置き換えるものではありません。 $1'$2などのようなものですが、キャプチャがいくつあるかわかりません。 (<%= t('$1') %>

まだ

完璧ではないと交換してより多くのあなたが必要があるかどう「の唯一のペアが、あります前提

<%= t\("([^"]*)"\) %>

答えて

0

動作しませんあなたの正規表現は明らかである理由それは一つだけをキャプチャします。と他のすべてをスキップしますが、"がある各位置で停止する必要があります。 \G tokenで行うことができます。

崇高なテキストの正規表現は、正規表現以下(PCRE)Perl互換であるため、この問題への適切なソリューションです:

正規表現:

<%(?:(?!%>)[^"])*\K"|(?!\A)\G(?:(?!%>)[^"\\])*(?:\\.(?:(?!%>)[^"\\])*)*\K" 

置換文字列:'

Live demo

説明:

<%      # Match beginning of Ruby tag 
(?:(?!%>)[^"])*   # Match any thing except a double quote - caring not to jump over closing tag (zero or more times) 
\K"      # Up to a double quote (\K reset all match so far) 
|      # OR 
(?!\A)\G    # Assert end of previous match 
(?:(?!%>)[^"\\])*  # Match any thing except a double quote and backslash - caring not to jump over closing tag (zero or more times) 
(?:      # Start of non-capturing group (c) 
    \\.      # Match an escaped character 
    (?:(?!%>)[^"\\])*  # Match any thing except a double quote and backslash - caring not to jump over closing tag (zero or more times) 
)*      # End of non-capturing group (c) (zero or more times) 
\K"      # Up to a double quote (\K reset all match so far) 

崇高テキストプレビュー:

enter image description here

+0

私はactully '\ K'を使用していません。新しいことを学ぶ:) – Frexuz

0

私は崇高使用していますが、このようなvscodeなものではありませんが動作します実行するために)再び置き換える:

<%=([^"]*)"([^"]*)"([^"]*)%> =><%=$1'$2'$3%>

+0

更新例。私は、特にt()メソッドではなく、ルビタグの二重引用符がすべて出現したと言っていました。 – Frexuz

関連する問題