正则表达式复制一个单词并添加额外的文本 [英] regex to duplicate a word and add in extra text

查看:25
本文介绍了正则表达式复制一个单词并添加额外的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一长串想要复制的单词

I have a long list of words that I want to duplicate

例子

CallDateTime
WebDateTime
WavName
Dnis
Verified
Concern
ConcernCode

我正在尝试理解一些正则表达式来复制每个单词并放置在右侧,同时添加一些需要的文本

I'm trying to understand some regex to copy each word and placing to the right, along with adding in some needed text

's/(\t+)_(\w+)/\u\2, \u\1, \0/'

好吧..这不起作用,这是需要的预期输出

@CallDateTime = i.CallDateTime,
@WebDateTime = i.WebDateTime,

等等……

显然,用@ 添加 ^ 很容易,用 , 添加 $,但我也想用正则表达式复制

Obviously adding in ^ with @ is easy and $ with , , but I want to also copy with a regex

我见过这个

 ((\w+)_(\w+))
Replace Pattern:
\3, \2, \1

但我不明白..

推荐答案

让我们用notepad++来解决这个问题:

Let's solve this with notepad++:

查找内容:(\w+)
替换为:@\1 = i.\1,

说明:

  • \w+ 匹配一个或多个单词字符
  • (...) 是一个捕获组.您可以在替换部分使用 \1 引用它
  • 替换:文字 @,然后是捕获的单词,然后是空格等...
  • \w+ matches one or more word characters
  • (...) is a capturing group. You can reference it with \1 in the replacement part
  • replacement: A literal @, then the captured word, then a space, etc...

这篇关于正则表达式复制一个单词并添加额外的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆