java - Regular expression to get characters before brackets or comma -
i'm pulling hair out bit this.
say have string 7f8hd::;;8843fdj fls "": ] fjisla;vofje]]} fd)fds,f,f
i want extract 7f8hd::;;8843fdj fls "": string based on premise string ends either } or ] or , or ) characters present need first one.
i have tried without success create regular expression matcher , pattern class can't seem right.
the best come below reg exp doesn't seem work think should.
string line = "7f8hd::;;8843fdj fls "": ] fjisla;vofje]]} fd)fds,f,f"; matcher m = pattern.compile("(.*?)\\}|(.*?)\\]|(.*?)\\)|(.*?),").matcher(line); while (matcher.find()) { system.out.println(matcher.group()); } i'm not understanding reg exp correctly. great.
^[^\]}),]* matches start of string until (but excluding) first ], }, ) or ,.
in java:
pattern regex = pattern.compile("^[^\\]}),]*"); matcher regexmatcher = regex.matcher(line); if (regexmatcher.find()) { system.out.println(regexmatcher.group()); } (you can remove backslashes ([^]}),]), keep them there clarity , compatibility since not regex engines recognize idiom.)
explanation:
^ # match start of string [^\]}),]* # match 0 or more characters except ], }, ) or ,
Comments
Post a Comment