![](/img/trans.png)
[英]How to know if a string could match a regular expression by adding more characters
[英]Adding characters conditionally to a regular expression match
我在Eclipse中使用正则表达式,并想知道是否有一种方法可以根据匹配添加字符。
我正在使用这些表达式来匹配和替换:
Match: ^(\s*)(//)?(.*?)"([\p{Punct}\p{Space}]*)?(\p{Alnum}.*?\p{Alnum})([\p{Punct}\p{Space}]*)?"(.*?)$
Replace: $1$3"$4" \+ i18n.tr\("$5"\) \+ "$6"$7
例如
System.err.println("Unexpected number of guests: ");
我试图用这个替换它
System.err.println(i18n.tr("Unexpected number of guests") + ": ");
但我得到了
System.err.println("" + i18n.tr("Unexpected number of guests") + ": ");
如果没有捕获任何东西,有没有办法摆脱“+ +前面的i18n.tr(。*)?
你不能通过一次搜索替换来做到这一点!
唯一的方法是使用两个搜索替换:
Match: ^(\\s*)(//)?(.*?)"([\\p{Punct}\\p{Space}]++)(\\p{Alnum}.*?\\p{Alnum})([\\p{Punct}\\p{Space}]*)?"(.*?)$
Replace: $1$3"$4" \\+ i18n.tr\\("$5"\\) \\+ "$6"$7
Match: ^(\\s*)(//)?(.*?)"(\\p{Alnum}.*?\\p{Alnum})([\\p{Punct}\\p{Space}]*)?"(.*?)$
Replace: $1$3i18n.tr\\("$4"\\) \\+ "$5"$6
不要忘记在任何尝试之前进行备份
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.