[英]Regex Get all characters after first slash using java
I am trying to get all the characters after first slash. 我试图在第一个斜杠后得到所有字符。
I have tried the following but its not working 我尝试了以下方法,但无法正常工作
Can anyone share any sample regex to get all characters after first slash 任何人都可以共享任何正则表达式示例以在第一个斜杠后获取所有字符
String pattern = "[A-Za-z\\s]|(?<=[A-Za-z\\s])/";
sample input - xx/abs-12345/67890 样本输入-xx / abs-12345 / 67890
expected output - abs-12345/67890 预期输出-ABS-12345 / 67890
This is the regex you need: 这是您需要的正则表达式:
(?<=/).+
The lookbehind looks for a /
and then matches every non-line-ending after it. 后面的查找将查找/
,然后匹配其后的每个非行末尾。 Since the regex engine checks from left to right, it will always find the first slash. 由于正则表达式引擎从左到右进行检查,因此它将始终找到第一个斜杠。
Matcher m = Pattern.compile("(?<=/).+").matcher("xx/abs-12345/67890");
if (m.find()) {
System.out.println(m.group());
}
You can even do this in one line, without regex. 您甚至可以在一行中执行此操作,而无需使用正则表达式。
System.out.println(Arrays.stream(s.split("/")).skip(1).collect(Collectors.joining("/")));
s
is the input string. s
是输入字符串。
I think you don't need a regex to do that job. 我认为您不需要正则表达式即可完成这项工作。 You can do like below. 您可以像下面这样。
String s = "123/abc23r";
String strAfterFirstSlash = s.substring(s.indexOf('/')+1,s.length());
char[] charsAfterFirstSlash = strAfterFirstSlash.toCharArray(); // If you need to get result in a char array
System.out.println(strAfterFirstSlash);
Output: 输出:
abc23r
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.