[英]Regex to find Integers in particular string lines
I have this regex to find integers in a string (newlines). 我有这个正则表达式来查找字符串(换行符)中的整数。 However, I want to filtrate this. 但是,我想过滤它。 I want the regex to find the number in certain lines, and not others. 我希望正则表达式在某些行而不是其他行中查找数字。
String: 串:
String test= "ytrt.ytrwyt.ytreytre.test1,0,2,0"
+"sfgtr.ytyer.qdfre.uyeyrt.test2,0,8,0"
+"sfgtr.ytyer.qdfre.uyeyrt.test3,0,3,0";
pattern = "(?<=,)\\d+";
pr = Pattern.compile(pattern);
match = pr.matcher(test);
System.out.println();
if (match.find()) {
System.out.println("Found: " + match.group());
}
This regex find the integers after the comma, for all the lines. 此正则表达式在所有行的逗号后面找到整数。 If I want a particular regex to find the integers in the line containing "test1", "test2", and "test3". 如果我想让某个正则表达式在包含“ test1”,“ test2”和“ test3”的行中找到整数。 How should I do this? 我应该怎么做? I want to create three different regex, but my regex skills are weak. 我想创建三个不同的正则表达式,但是我的正则表达式技能很弱。
First regex should print out 2. The second 8 and the third 3. 第一个正则表达式应打印出2。第二个8和第三个3。
You can expand your pattern to include test[123]
in the lookbehind, which would match test1
, test2
, or test3
: 您可以扩展模式以在后面的外观中包含test[123]
,它将与test1
, test2
或test3
匹配:
String pattern = "(?<=test[123][^,]{0,100},[^,]{1,100},)\\d+";
Pattern pr = Pattern.compile(pattern);
Matcher match = pr.matcher(test);
System.out.println();
while (match.find()) {
System.out.println("Found: " + match.group());
}
The ,[^,]
portion skis everything between two commas that follow testN
. ,[^,]
部分滑雪了在testN
两个逗号之间的所有内容。
I use {0,100}
in place of *
and {1,100}
in place of +
inside lookbehind expressions, because Java regex engine requires that lookbehinds had a pre-defined limit on their length. 我使用{0,100}
代替*
并使用{1,100}
代替lookbehind表达式中的+
,因为Java正则表达式引擎要求lookbehinds对其长度有预先定义的限制。 If you need to allow skipping more than 100 characters, adjust the maximum length accordingly. 如果需要允许跳过100个以上的字符,请相应地调整最大长度。
You can use the following Pattern
and loop for this: 您可以为此使用以下Pattern
和循环:
String test= "ytrt.ytrwyt.ytreytre.test1,0,2,0"
+ System.getProperty("line.separator")
+"sfgtr.ytyer.qdfre.uyeyrt.test2,0,8,0"
+ System.getProperty("line.separator")
+"sfgtr.ytyer.qdfre.uyeyrt.test3,0,3,0";
// | "test" literal
// | | any number of digits
// | | | comma
// | | | any number of digits
// | | | | comma
// | | | | | group1, your digits
Pattern p = Pattern.compile("test\\d+,\\d+,(\\d+)");
Matcher m = p.matcher(test);
while (m.find()) {
// prints back-reference to group 1
System.out.printf("Found: %s%n", m.group(1));
}
Output 产量
Found: 2
Found: 8
Found: 3
You could also use capturing groups to extract the test number and the other number from the string: 您还可以使用捕获组从字符串中提取测试编号和其他编号:
String pattern = "test([123]),\\d+,(\\d+),";
...
while (match.find()) {
// get and parse the number after "test" (first capturing group)
int testNo = Integer.parseInt(match.group(1));
// get and parse the number you wanted to extract (second capturing group)
int num = Integer.parseInt(match.group(2));
System.out.println("test"+testNo+": " + num);
}
Which prints 哪些印刷品
test1: 2
test2: 8
test3: 3
Note: In this example parsing the strings is only done for demonstration purposes, but it could be useful, if you want to do something with the numbers, like storing them in a array. 注意:在此示例中,仅出于演示目的完成字符串的解析,但是如果您想对数字进行某些操作(例如将它们存储在数组中),则可能很有用。
Update: If you also want to match strings like "ytrt.ytrwyt.test1.ytrwyt,0,2,0"
you could change pattern
to "test([123])\\\\D*,\\\\d+,(\\\\d+),"
to allow any number of non-digits to follow test1
, test2
or test3
(preceding the comma seperated ints). 更新:如果您还想匹配"ytrt.ytrwyt.test1.ytrwyt,0,2,0"
类的字符串"ytrt.ytrwyt.test1.ytrwyt,0,2,0"
可以将pattern
更改为"test([123])\\\\D*,\\\\d+,(\\\\d+),"
以允许任意数量的非数字跟在test1
, test2
或test3
(在逗号分隔的整数之前)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.