[英]regular expression how to get substring?
This are my example data:这是我的示例数据:
406/FG-2000
411/FA-2120
XX-226/2012-DFDF
OASDV-279/1016-FDFFD
how can with regex (only regex) in java i parse我如何在 java 中使用正则表达式(仅正则表达式)解析
406/FG-2000 -> 406 (from begining to /)
411/FA-2120 -> 411 (from begining to /)
XX-226/2012-DFDF -> 226 (from - to /)
OASDV-279/1016-FDFFD -> 279 (from - to /)
this are two rules.这是两条规则。 (from begining to /) or (from - to /)
(从开始到 /)或(从 - 到 /)
This code does what you asked for:此代码执行您的要求:
input.replaceAll(".*(^|-)(.*?)/.*", "$2")
If you want to restrict it to digits only, change the matching regex to ".*(^|-)(\\d*?)/.*"
如果您只想将其限制为数字,请将匹配的正则表达式更改为
".*(^|-)(\\d*?)/.*"
Here's a test:这是一个测试:
public static void main(String[] args) {
String[] inputs = { "406/FG-2000", "411/FA-2120", "XX-226/2012-DFDF", "OASDV-279/1016-FDFFD" };
for (String input : inputs)
System.out.println(input.replaceAll(".*(^|-)(.*?)/.*", "$2"));
}
Output: Output:
406
411
226
279
Try this one:试试这个:
(^|-)[0-9]+/
It will match any numbers from the beginning or from a - to the /它将匹配从开头或从 - 到 / 的任何数字
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.