0

I have an arraylist links. All links having same format abc.([a-z]*)/\\d{4}/

List<String > links= new ArrayList<>();
        links.add("abc.com/2012/aa");
        links.add("abc.com/2014/dddd");
        links.add("abc.in/2012/aa");

I need to get the last portion of every link. ie, the part after domain name. Domain name can be anything(.com, .in, .edu etc).

/2012/aa
/2014/dddd
/2012/aa

This is the output i want. How can i get this using regex? Thanks

Dinoop Nair
  • 2,663
  • 6
  • 31
  • 51

6 Answers6

3

Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems.

(see here for background)

Why use regex ? Perhaps a simpler solution is to use String.split("/") , which gives you an array of substrings of the original string, split by /. See this question for more info.

Note that String.split() does in fact take a regex to determine the boundaries upon which to split. However you don't need a regex in this case and a simple character specification is sufficient.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • +1 for nice solution and quote... but is it true the [quote is incomplete](http://www.rexegg.com/regex-humor.html) (second par, two-problem quote)? :) – zx81 Jun 12 '14 at 10:25
2

Try with below regex and use regex grouping feature that is grouped based on parenthesis ().

\.[a-zA-Z]{2,3}(/.*)

Pattern description :

dot followed by two or three letters followed by forward slash then any characters

DEMO

Sample code:

Pattern pattern = Pattern.compile("\\.[a-zA-Z]{2,3}(/.*)");
Matcher matcher = pattern.matcher("abc.com/2012/aa");

if (matcher.find()) {
    System.out.println(matcher.group(1));
}

output:

/2012/aa

Note:

You can make it more precise by using \\.[a-zA-Z]{2,3}(/\\d{4}/.*) if there are always 4 digits in the pattern.

Braj
  • 46,415
  • 5
  • 60
  • 76
1

FOR EXAMPLE

String s="abc.com/2014/dddd";
System.out.println(s.substring(s.indexOf('/')));

OUTPUT

/2014/dddd

Or you can go for split method.

System.out.println(s.split("/",2)[1]);//OUTPUT:2014/dddd --->you need to add /
akash
  • 22,664
  • 11
  • 59
  • 87
1
String result = s.replaceAll("^[^/]*","");

s would be the string in your list.

Kent
  • 189,393
  • 32
  • 233
  • 301
1

Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems.

Why not just use the URI class?

output = new URI(link).getPath()
Mikkel Løkke
  • 3,710
  • 23
  • 37
1

Try this one and use the second capturing group

(.*?)(/.*)

Esteban
  • 1,496
  • 17
  • 22