我想在一行中搜索字符串模式,如果找到则用匹配的字符串模式替换整行。
我的字符串模式以2个字母字符开头,后跟5或6个数字字符。防爆。 HR12345 或 HR123456
以下是带有图案的线条的示例。
Class cum accumsan. In. Pellentesque nec magna interdum fusce metus, massa aliquam HR032145
Amet commodo arcu, felis orci Per. Facilisis blandit rhoncus hac porttitor ut duis eu HR32145
Mattis quis magna, suspendisse HR32146 aucibus vel, fames Nonummy molestie penatibus ad.
Nascetur mattis ad egestas et nec HR032111 Penatibus posuere. Posuere.
Inceptos consectetuer neque nullam HR032114. rutrum Eleifend.
Netus tortor conubia parturient sapien interdum adipiscing sociis luctus integer HR032113
HR032112 Mattis erat a ante. Rutrum. Mattis risus fames. Euismod sapien morbi habitasse.
Platea sapien vitae Risus. Erat dictum elit dapibus convallis.
Facilisis ut dis morbi integer fusce dolor Et class Primis iaculis.
Aptent per risus phasellus HR032188
搜索后,替换它应该看起来像
HR032145
HR32145
HR32146
HR032111
HR032114
HR032113
HR032112
Platea sapien vitae Risus. Erat dictum elit dapibus convallis.
Facilisis ut dis morbi integer fusce dolor Et class Primis iaculis.
HR032188
答案 0 :(得分:0)
尝试以下简单查找和替换:
<强>查找强>
public class Main {
String x = "a";
public static void main(String[] args) {
new Main();
}
public Main(){
System.out.println(x); //"a"
String x = "b";
System.out.println(x); //"b"
new Thread(new Runnable() {
public void run() {
String x = "c";
System.out.println(x); //"c"
}
}).start();
System.out.println(x); //"b"
}
public void method(){
System.out.println(x); //"a"
}
}
<强>替换强>
^.*(HR\d+).*$
只有包含$1
后跟一个或多个数字的行才会进行此替换。因此,没有这种模式的线条甚至不会匹配,也不会在那里进行替换。