2

I'm currently using http://www.regexr.com/ and the Strings I'm trying to parse through are in the format of 133a1d6a-f4fa-49ba-928d-0f4c943ce5d3/File-20140805-013806693.pdf.

I'm trying to get only the portion after the / and before .pdf.

My current regex pattern I have is: /\/([A-Za-z0-9-])+/g

which gives me: /File-20140805-013806693

How do I make the pattern omit the / AND the file type only matching File-20140805-013806693

My next step is to put this into java code while iterating through a loop of these Strings.

Any help would be appreciated!

Richard
  • 826
  • 7
  • 23
  • 41

3 Answers3

2

Try using:

(?<=\/)[A-Za-z0-9-]+
Elkfrawy
  • 353
  • 2
  • 10
  • 1
    I removed the grouping parentheses, I used them as snapshot from your solution, I just added the part to exclude the forward slash. – Elkfrawy Aug 07 '14 at 22:09
1

This seems to be what you are looking for.

.*\/([^./]+).[\w\d]+$

Regular expression visualization

Debuggex Demo

This regex gets all the file extensions if you just want .pdf do this.

.*\/([^./]+).pdf$
progrenhard
  • 2,333
  • 2
  • 14
  • 14
  • Nice, this works in javascript but realized that the tool I was using would not work with java. Any idea how it would convert to java? – Richard Aug 07 '14 at 21:36
  • 1
    @Richard Look at this post on how to compile and use capture groups with java. http://stackoverflow.com/questions/6865377/java-regex-capture-group Rember index 0 captures the entire stirng in the capture statement. So in this case the captured group that you would want would be at `group(1)` – progrenhard Aug 07 '14 at 21:50
0

You can use a combination of lookahead and lookbehind:

/(?<=\/).*?(?=\.pdf)/g

You can test it here.

enrico.bacis
  • 30,497
  • 10
  • 86
  • 115