Eclipse中使用正则表达式搜索替换

分类:software | 标签: 正则表达  替换  eclipse 
2011-11-29 11:28 阅读(1930)评论(0)编辑删除

最近在eclipse开发中用到正则表达式搜索替换,顺便总结。

搜索:^(.*)<h:outputText.*value=.*"#\{(.*)\}"(.*)$
替换:$1<h:outputText value= "#{strings.trim($2,30)}"$3

实现的功能是能找到代码中类似:

<h:outputText value="#{appl.departmentName}" />

的行。并替换为:

<h:outputText value= "#{strings.trim(appl.applicantName,30)}" />

在上一组搜索表达式基础上作了些改进:

搜索:^(.*)<h:outputText(.*\R??.*)value=\p{Space}*"#\{(.*)\}"(.*)$
替换:$1<h:outputText$2value="#{strings.trim($3,30)}"$4

这组条件考虑到了中间带换行的情况。

这种正则表达式的编写有3个问题要注意:

1.有些特殊字符需要转意。例如:{  (   这种字符被正则表达式语法赋予了新意义,因此要用\{  \( 来代表原字符。

2.用( )对来截取字段中需要保留的变化部分。再用$1 , $2 , $3 ...$n 在替换字段里复用他们。

3.注意eclipse里的换行是 \R  ,不是下面文档里的 \r  。

以下是正则表达式语法的详细列举:

Construct Matches  Characters Character classes Predefined character classes POSIX character classes (US-ASCII only) Classes for Unicode blocks and categories Boundary matchers Greedy quantifiers Reluctant quantifiers Possessive quantifiers Logical operators Back references Quotation Special constructs (non-capturing)

x The character x
\\ The backslash character
\0n The character with octal value 0n (0 <= n <= 7)
\0nn The character with octal value 0nn (0 <= n <= 7)
\0mnn The character with octal value 0mnn (0 <= m <= 3, 0 <= n <= 7)
\xhh The character with hexadecimal value 0xhh
\uhhhh The character with hexadecimal value 0xhhhh
\t The tab character (&apos;\ &apos;)
\n The newline (line feed) character (&apos;\ &apos;)
\r The carriage-return character (&apos;\ &apos;)
\f The form-feed character (&apos;\ &apos;)
\a The alert (bell) character (&apos;\\u0007&apos;)
\e The escape character (&apos;\\u001B&apos;)
\cx The control character corresponding to x
[abc] a, b, or c (simple class)
[^abc] Any character except a, b, or c (negation)
[a-zA-Z] a through z or A through Z, inclusive (range)
[a-d[m-p]] a through d, or m through p: [a-dm-p] (union)
[a-z&&[def]] d, e, or f (intersection)
[a-z&&[^bc]] a through z, except for b and c: [ad-z] (subtraction)
[a-z&&[^m-p]] a through z, and not m through p: [a-lq-z](subtraction)
. Any character (may or may not match line terminators)
\d A digit: [0-9]
\D A non-digit: [^0-9]
\s A whitespace character: [ \t\n\x0B\f\r]
\S A non-whitespace character: [^\s]
\w A word character: [a-zA-Z_0-9]
\W A non-word character: [^\w]
\p{Lower} A lower-case alphabetic character: [a-z]
\p{Upper} An upper-case alphabetic character:[A-Z]
\p{ASCII} All ASCII:[\x00-\x7F]
\p{Alpha} An alphabetic character:[\p{Lower}\p{Upper}]
\p{Digit} A decimal digit: [0-9]
\p{Alnum} An alphanumeric character:[\p{Alpha}\p{Digit}]
\p{Punct} Punctuation: One of !"#$%&&apos;()*+,-./:;<=>?@[\]^_`{|}~
\p{Graph} A visible character: [\p{Alnum}\p{Punct}]
\p{Print} A printable character: [\p{Graph}]
\p{Blank} A space or a tab: [ \t]
\p{Cntrl} A control character: [\x00-\x1F\x7F]
\p{XDigit} A hexadecimal digit: [0-9a-fA-F]
\p{Space} A whitespace character: [ \t\n\x0B\f\r]
\p{InGreek} A character in the Greek block (simple block)
\p{Lu} An uppercase letter (simple category)
\p{Sc} A currency symbol
\P{InGreek} Any character except one in the Greek block (negation)
[\p{L}&&[^\p{Lu}]]  Any letter except an uppercase letter (subtraction)
^ The beginning of a line
$ The end of a line
\b A word boundary
\B A non-word boundary
\A The beginning of the input
\G The end of the previous match
\Z The end of the input but for the final terminator, if any
\z The end of the input
X? X, once or not at all
X* X, zero or more times
X+ X, one or more times
X{n} X, exactly n times
X{n,} X, at least n times
X{n,m} X, at least n but not more than m times
X?? X, once or not at all
X*? X, zero or more times
X+? X, one or more times
X{n}? X, exactly n times
X{n,}? X, at least n times
X{n,m}? X, at least n but not more than m times
X?+ X, once or not at all
X*+ X, zero or more times
X++ X, one or more times
X{n}+ X, exactly n times
X{n,}+ X, at least n times
X{n,m}+ X, at least n but not more than m times
XY X followed by Y
X|Y Either X or Y
(X) X, as a capturing group
\n Whatever the nth capturing group matched
\ Nothing, but quotes the following character
\Q Nothing, but quotes all characters until \E
\E Nothing, but ends quoting started by \Q
(?:X) X, as a non-capturing group
(?idmsux-idmsux)  Nothing, but turns match flags on - off
(?idmsux-idmsux:X)   X, as a non-capturing group with the given flags on - off
(?=X) X, via zero-width positive lookahead
(?!X) X, via zero-width negative lookahead
(?<=X) X, via zero-width positive lookbehind
(?<!X) X, via zero-width negative lookbehind
(?>X) X, as an independent, non-capturing group

Eclipse中使用正则表达式搜索替换的更多相关文章

  1. 在eclipse中利用正则表达式查找替换

    众所周知,eclipse是可以用正则表达式来进行查找的,那么怎么利用正则表达式进行替换呢? 方法也很简单,就是在Replace with: 里面输入$来代表捕获型括号的匹配结果,$1为第一个匹配结果, ...

  2. VIM中的正则表达式及替换命令

    VIM中的正则表达式及替换命令 一.使用正则表达式的命令 使用正则表达式的命令最常见的就是 / (搜索)命令.其格式如下: /正则表达式 另一个很有用的命令就是 :s(替换)命令,将第一个//之间的正 ...

  3. 关于在Eclipse中使用正则表达式替换的一点记录(使用正则表达式的分组)

    今天在工作中遇到了点替换的麻烦事,由于数据类进行了变动,具体情况是这样的,需要将下面的代码: player.skillData[i].name 替换为: player.skillData.getSki ...

  4. less和vim中使用正则表达式搜索

    使用less查看 txt 文件之后,按\可以正则表达式来搜索: less phonelist.txt (232) 298-2265 (624) 381-1078 (540) 126-1980 (874 ...

  5. 在eclipse中使用正则表达式进行搜素

  6. IDEA中通过正则表达式批量替换空白行

    快捷键Ctrl+r 填入^\s*\n,勾选Regex,Replace all

  7. 在eclipse中使用git创建本地库,以及托管项目到GitHub超详细教程

    关于安装git的教程,由于比较简单,并且网上教程特别多,而且即使不按照网上教程,下载好的windows版本git,安装时候一路默认设置就行. 安装好之后,在桌面上有git图标:右键菜单中有Git Ba ...

  8. 在UltraEdit的查找和替换中使用正则表达式 (转)

    很多朋友都用过或者正在用UltraEdit,这个编辑器陪伴我也好几年了,从很多地方影响着我写代码的快捷键习惯,Ultraedit提供了非常丰富的编辑功能,其中非常重要的查找和替换功能一定大家都用过,U ...

  9. UltraEdit中使用正则表达式替换

    UltraEdit在使用正则表达式进行查找替换时有两个可使用的语法集合.一个是 UltraEdit 的更早的版本被使用的原来的 UltraEdit 句法.另一个是"Unix"类型的 ...

随机推荐

  1. 20145103 《Java程序设计》第3周学习总结

    20145103 <Java程序设计>第3周学习总结 教材学习内容总结 第四章我首先了解了CPU与内存的关系,栈与堆的关系.要产生对象必须先定义类,类是对象的设计图,对象是累的实例.以类名 ...

  2. mysql 存储过程 -- 游标的使用(备忘)

    BEGIN ; DECLARE f_ratio FLOAT DEFAULT 0.8; ); ); DECLARE i_statDate DATE; DECLARE i_accumulateCount ...

  3. java 24点算法实现

    最近闲来无事,突然怀念起小时候和堂兄表姐们经常玩24点游戏,于是就琢磨着是不是开发一个安卓手机版本.然后上网上一搜,发现已经被别人给开发烂了啊.不过这只能说明这个小游戏要想赚广告费很难了,但是拿来锻炼 ...

  4. 20、android解决方案(转载)

    目录: 1.广告 2.推送 3.云 4.统计 5.后端存储 6.地图 7.测试 8.托管 9.支付 10.音视频 11.社会化分享 12.存储 13.自动更新 14.轻开发 15.安全 16.图像 1 ...

  5. 读书笔记:<我是一只IT小小鸟>

    <我是一只IT小小鸟>第一次听到这本书的时候,我便有了深深的好奇,虽然我是一名学习软件工程的大学生,但是还是第一次听到“IT”这个名词,既陌生又好奇.听到老师提起了这本书的意义以及看法,我 ...

  6. 【递推】BZOJ 4300:绝世好题

    4300: 绝世好题 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 564  Solved: 289[Submit][Status][Discuss] ...

  7. 【BZOJ】【1855】【SCOI2010】/【HDOJ】【3401】股票交易

    DP/单调队列优化 题解:http://www.cnblogs.com/jianglangcaijin/p/3799736.html 令f[i][j]表示第 i 天结束后,手里剩下 j 股的最大利润, ...

  8. 【BZOJ】【3166】【HEOI2013】Alo

    可持久化Trie+set Orz zyf…… 搞区间中次大值不好搞,那么我们就反过来,找一个数,然后看它在哪些区间里是次大值…… (然而事实上我们并不用真的把这个区间具体是什么找见,只要知道它可以跟哪 ...

  9. 初用Spring Test

    粗糙的研究了下Spring test,分享以下blog: 1. http://blog.csdn.net/shan9liang/article/details/40452469 2. http://w ...

  10. Leetcode#127 Word Ladder

    原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...