Solved

Java regex needed

Posted on 2011-09-21
9
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

734 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