【问题标题】:sublime text find, copy, change and paste in new linesublime text 在新行中查找、复制、更改和粘贴
【发布时间】:2014-05-09 06:22:35
【问题描述】:

我需要一个正则表达式来查找文档中的特定文本并更改并粘贴到新行中。

logging.info("hello i am here")

需要找到所有出现的 logging.info 并更改为

print("hello i am here")

最终是这样的

logging.info("hello i am here")
print("hello i am here")

他们是我可以做或需要手动做的任何正则表达式。

【问题讨论】:

  • 老板我需要将它粘贴到新行而不是查找和替换。我想你明白我的意思了。
  • 好的...现在我明白你的意思了。说need to find all occurrence of logging.info and change to ... 的部分让我失望了。你有没有尝试过什么? logging.info 总是单独在线吗?是否有要保留的缩进?
  • 是的,现在找到了我。是的,缩进是必须的。因为我正在使用 python 代码。

标签: regex sublimetext3


【解决方案1】:

我相信这样的正则表达式应该可以工作:

^(\h*)logging\.info\(([^)]*)\)

替换为:

$0\n$1print($2)

regex101 demo

说明:

^                 # Beginning of line
(\h*)             # Get any spaces/tabs before the line and store in $1
logging\.info\(   # Match 'logging.info('
([^)]*)           # Get everything between parens
\)                # Match closing paren

请注意,上述正则表达式假定logging.info 函数中没有其他括号。

替换的意思:

$0                # Whole match
\n                # Newline
$1                # Place the indentation
print(            # 'print('
$2                # The part within the `logging.info` function
)                 # Closing paren

【讨论】:

  • @user2190040 你检查正则表达式模式了吗?它是带有.* 的顶部最左边的按钮。这是我的 ST3 版本:beforeafter
  • 是的,它的工作 ^(\h*)logging\.info(([^)]*)) 以前我正在尝试 ^([ ]*)logging\.info(([^)] *))
  • @user2190040 哦,一定是您使用了制表符。我最初在正则表达式中只有空格,但后来才想到制表符。对此感到抱歉:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
  • 1970-01-01
  • 2021-07-02
  • 1970-01-01
  • 2014-01-29
相关资源
最近更新 更多