regex - Splitting a Java String on the "+" symbol gives an error -
i'm writing java program, 1 of sub-tasks take string , split @ location. so, here's 1 little snippet of code:
public class driver { public static void main(string[] args) { string str = "a+d"; string[] strparts = str.split("+"); (string item : strparts) { system.out.println(item); } } }
i expecting result:
a d
however, surprise, get:
exception in thread "main" java.util.regex.patternsyntaxexception: dangling meta character '+' near index 0 + ^ @ java.util.regex.pattern.error(pattern.java:1955) @ java.util.regex.pattern.sequence(pattern.java:2123) @ java.util.regex.pattern.expr(pattern.java:1996) @ java.util.regex.pattern.compile(pattern.java:1696) @ java.util.regex.pattern.<init>(pattern.java:1351) @ java.util.regex.pattern.compile(pattern.java:1028) @ java.lang.string.split(string.java:2367) @ java.lang.string.split(string.java:2409) @ test.driver.main(driver.java:8)
what's going on? thanks!
+
special metacharacter in regex repeats previous character 1 or more times. need escape match literal +
string[] strparts = str.split("\\+");
java regex string split patternsyntaxexception
No comments:
Post a Comment