Solved

Java regex needed

Posted on 2011-09-21
9
266 Views
Last Modified: 2012-05-12
Hi,

I need a regex to match the following situations. Thanks!

A. The last segment of (1)-(3) show increased numeric pieces:
1) http://www.binggo.com/shipin/20207011122957
2) http://www.binggo.com/shipin/202070111
3) http://www.binggo.com/shipin/202070

B. The last segment of (4)-(5) begins with a\d+.html
4) http://www.binggo.com/shipin/a0207.html
5) http://www.binggo.com/shipin/a0207b111c2957.html

C) The last segment of (6) are \d+.html
6) http://www.binggo.com/shipin/20601081184657.html

D) The (7)-(10) are much more difficult, and I can figure out a regex for it.
7) http://www.binggo.com/shipin/a20209b2020904h2616/315432.html
8) http://www.binggo.com/shipin/a20209b2020904h2616/
9) http://www.binggo.com/shipin/a20209b2020904/
10) http://www.binggo.com/shipin/a20209/
0
Comment
Question by:wsyy
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36578294

So do you want in all cases to return everything after "shipin/" ?

I think your 5) will not match a\d+.html either
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36578385
         String [] urlShips = {


 "http://www.binggo.com/shipin/20207011122957",
 "http://www.binggo.com/shipin/202070111",
 "http://www.binggo.com/shipin/202070",


"http://www.binggo.com/shipin/a0207.html",
 "http://www.binggo.com/shipin/a0207b111c2957.html",


 "http://www.binggo.com/shipin/20601081184657.html",


 "http://www.binggo.com/shipin/a20209b2020904h2616/315432.html",
 "http://www.binggo.com/shipin/a20209b2020904h2616/",
"http://www.binggo.com/shipin/a20209b2020904/",
 "http://www.binggo.com/shipin/a20209/",



         };

        for(String url : urlShips) {

            url = url.replaceAll(".+shipin/(.+)","$1");
            System.out.println(url);
            

        }

      

Open in new window


Output:
20207011122957
202070111
202070
a0207.html
a0207b111c2957.html
20601081184657.html
a20209b2020904h2616/315432.html
a20209b2020904h2616/
a20209b2020904/
a20209/

Open in new window

0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 62 total points
ID: 36578800
>>I need a regex to match the following situations.

1-3 are not regex territory
7-10 likewise

The rest are suitable for regex treatment:
final String PATTERN = ".*?\\d+\\.html";
boolean valid = urlString.matches(PATTERN);

Open in new window

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 63

Expert Comment

by:Zvonko
ID: 36578924
Your \d is not realy digit but hex: [\da-f]+
Do not forget the iGnoreCase modifier.

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36578930
Sory no hex.It has "h" and therefore it can be base64?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36593968
What are you trying to match/extract? The whole URL, or just parts of each?
0
 

Author Comment

by:wsyy
ID: 36593991
I am trying to match the whole url.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36594001
Please forgive my ignorance, but are you looking for one regex pattern to match all, or one per group (i.e. A, B, C, D)?
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 63 total points
ID: 36594009
While I await your response, I'll hazard a guess to say you want all to be matched. Please try the following:

String pattern = "http://www\\.binggo\\.com/shipin/(?:[a-z0-9]+\\.html|[a-z0-9]+(?:/(?:[a-z0-9]+\\.html)?)?)?";

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question