Notepad ++正则表达式

时间:2012-09-02 06:08:32

标签: regex notepad++

首先,正则表达式很可能是我每次处理过的最令人困惑的事情 - 据说我无法相信它们能让它们变得多么有效。

所以我试图理解通配符正则表达式没有运气

需要转

f_firstname
f_lastname
f_dob       
f_origincountry 
f_landing  

':f_firstname'=>$f_firstname,
':f_lastname'=>$f_lastname,
':f_dob'=>$f_dob,
':f_origincountry'=>$f_origincountry,   
':f_landing'=>$f_landing,

在答案中,您可以简要描述一下您正在使用的正则表达式,我一直在阅读这些教程,但是我们一直在思考。感谢。

3 个答案:

答案 0 :(得分:6)

编辑:正如Chris指出的那样,您可以通过清除目标字符串中可能存在的任何空白来改进正则表达式。我也用\w代替点,因为它比使用.更好的做法

Search: ^f_(\w+)\s*$
^     # start at the beginning of the line
f_    # look for f_
(\w+) # capture in a group all characters
\s*   # optionally skip over (don't capture) optional whitespace
$     # end of the line

Replace: ':f_\1'=>$f_\1,
':f_    # beginning of replacement string
\1      # the group of characters captured above
'=>$f_  # some more characters for the replace
\1,     # the capture group (again)

答案 1 :(得分:2)

Find: (^.*)

Replace with: ':$1'=>$$1,

答案 2 :(得分:1)

查找内容:

(f_\w+)

此处我们匹配f_后跟单词字符\w+(加号均值一次或多次)。将整个事物包含在括号中意味着我们可以在替换模式中引用该组

替换为:

':\1'=>$\1,

这只是你的结果短语,而不是硬编码我在\1中引用{{1}}来引用搜索中的组