Chris Edwards
Chris Edwards

Reputation: 1558

Prevent regex from matching slash characters

I have the following regex \/\/.*\/.*? and I am applying it to strings in this format: mongodb://localhost:27017/admin?replicaSet=rs

Based on the above the returned match is: //localhost:27017/ however, I do not want the //../ characters I only want the result to be: localhost:27017

What needs to be modified in order to achieve this, I am fairly new to regex building.

Edit: I am using Java 1.7 to execute this regex statement.

Upvotes: 3

Views: 1015

Answers (2)

William Moore
William Moore

Reputation: 3994

You can use a combination of lookbehinds and lookaheads to replace all the "/" like: (?<=\/\/).+.*(?=\/).*?

Will match only localhost:27017 from your query

Here the (?<=\/\/). is matching to anything after the "//" string and the (?=\/) is matching anything before the "/" string

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627488

You can use this replaceAll approach in Java if you do not want to use Matcher:

System.out.println("mongodb://localhost:27017/admin?replicaSet=rs".replaceAll("mongodb://([^/]*).*", "$1")); 

Here, I assume you have 1 occurrence of a mongodb URL. mongodb:// matches the sequence of characters literally, the ([^/]*) matches a sequence of 0 or more characters other than / and stores them in a capturing group 1 (we'll use the backreference $1 to this group to retrieve the text in the replacement pattern). .* matches all symbols up to the end of a one-line string.

See IDEONE demo

Or, with Matcher,

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

The regex here - (?<=//)[^/]* - matches again a sequence of 0 or more characters other than / (with [^/]*), but makes sure there is // right before this sequence. (?<=//) is a positive lookbehind that does not consume characters, and thus does not return them in the match.

Upvotes: 1

Related Questions