正则表达式复制单词并添加额外的文字

时间:2017-05-17 07:18:43

标签: regex notepad++

我有一长串要复制的单词

示例

CallDateTime
WebDateTime
WavName
Dnis
Verified
Concern
ConcernCode

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

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

嗯..这不起作用,这是预期的输出需求

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

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

我见过这个

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

但我不明白..

3 个答案:

答案 0 :(得分:4)

让我们用记事本++来解决这个问题:

找到:exports.config = { // seleniumAddress: 'http://127.0.0.1:9001/', resultJsonOutputFile: "./e2e_report/report.json", getPageTimeout: 60000, allScriptsTimeout: 500000, framework: 'custom', // path relative to the current config file frameworkPath: require.resolve('protractor-cucumber-framework'), capabilities: { 'browserName': 'chrome', chromeOptions: { args: [ '--start-maximized' ] } }, // Spec patterns are relative to this directory. specs: [ './features/*.feature' ], baseURL: 'https://angularjs.org/', cucumberOpts: { require: ["./features/globals.js", './features/steps/*.js'], tags: [], format: 'pretty', profile: false, 'no-source': true, "compiler": [] //, //format:"json:./e2e_report/report.json" } };
替换为:(\w+)

说明:

  • @\1 = i.\1,匹配一个或多个单词字符
  • \w+是一个捕获组。您可以在替换部分
  • 中使用(...)引用它
  • 替换:文字\1,然后是捕获的单词,然后是空格等......

答案 1 :(得分:2)

替换:
\b(\w+)\b
通过
@\1 = i.\1

Javascript代码:

var str = "CallDateTime\nWebDateTime\nWavName\nDnis\nVerified\nConcern\nConcernCode";
str = str.replace(/\b(\w+)\b/g, '@$1 = i.$1');
console.log(str);

答案 2 :(得分:1)

搜索.+并将其替换为@$0 = i.$0,应该可以胜任。

https://regex101.com/r/WQXFy6/3