Solved

Java regex needed

Posted on 2011-09-21
9
289 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

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!

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

628 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