java - Prevent regex from matching slash characters -


i have following regex \/\/.*\/.*? , applying strings in format: mongodb://localhost:27017/admin?replicaset=rs

based on above returned match is: //localhost:27017/ however, not want //../ characters want result be: localhost:27017

what needs modified in order achieve this, new regex building.

edit: using java 1.7 execute regex statement.

you can use replaceall approach in java if not want use matcher:

system.out.println("mongodb://localhost:27017/admin?replicaset=rs".replaceall("mongodb://([^/]*).*", "$1"));  

here, assume have 1 occurrence of mongodb url. mongodb:// matches sequence of characters literally, ([^/]*) matches sequence of 0 or more characters other / , stores them in capturing group 1 (we'll use backreference $1 group retrieve text in replacement pattern). .* matches symbols end of one-line string.

see ideone demo

or, matcher,

pattern ptrn = pattern.compile("(?<=//)[^/]*"); matcher matcher = ptrn.matcher(str); while (matcher.find()) {    system.out.println(matcher.group()); } 

the regex here - (?<=//)[^/]* - matches again sequence of 0 or more characters other / (with [^/]*), makes sure there // right before sequence. (?<=//) positive lookbehind not consume characters, , not return them in match.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -