?
Solved

java pattern matcher find string before the matching

Posted on 2010-11-10
9
Medium Priority
?
321 Views
Last Modified: 2012-05-10
I have a string like this:
/temp/2345183/one:page/jiie200u3uuc

I need to take everything before /one:page (the number of fields might vary before /this:page).  What's the best way to do this?

In this example, I need find the "/temp/2345183" string.

Thanks!
0
Comment
Question by:mybluegrass
[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
9 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34102229
The following pattern should suffice. You text will be in group 1.
(?s)^(.*?)/one:page

Open in new window

0
 
LVL 1

Author Comment

by:mybluegrass
ID: 34103023
I tried this but it kept complaining no match found:

path = /temporary/cm/94d0bfb0_5ead_49c0/one:page/3bbfa669_c201_4b8

Pattern p = Pattern.compile("(?s)^(.*?)/one:page.*");
        Matcher m = p.matcher(path);
        docPath = m.group(1);
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34103614
Not sure why that's not working. Let's try a different route. I tested this in NetBeans and it worked as intended:
String path = "/temporary/cm/94d0bfb0_5ead_49c0/one:page/3bbfa669_c201_4b8";
Pattern p = Pattern.compile("/one:page.*");
Matcher m = p.matcher(path);
String docPath = m.replaceAll("");
System.out.println(docPath);

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 75

Expert Comment

by:käµfm³d 👽
ID: 34103653
The previous one didn't work because you have to call find() prior to accessing the group:
String path = "/temporary/cm/94d0bfb0_5ead_49c0/one:page/3bbfa669_c201_4b8";
Pattern p = Pattern.compile("(?s)^(.*?)/one:page");
Matcher m = p.matcher(path);
m.find();
String docPath = m.group(1);
System.out.println(docPath);

Open in new window

0
 
LVL 16

Expert Comment

by:jmatix
ID: 34103663
Try this:


  String sourcestring = "/temp/2345183/one:page/jiie200u3uuc";

  Pattern re = Pattern.compile("(.+)\\/one:page");

  Matcher m = re.matcher(sourcestring);
  int mIdx = 0;
    if (m.find())
        System.out.println( m.group(1));
    }

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34103951
You could simplify that to
s = s.replaceAll("(.*?)/one:page.*", "$1");

Open in new window

0
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 34107021
try this:

String fields = s.replaceAll("/one:page.*", "");
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 100 total points
ID: 34107301
Or
String fields = s.substring(0, s.indexOf("/one:page"));
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34158529
:)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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 …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

770 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