Solved

java pattern matcher find string before the matching

Posted on 2010-11-10
9
312 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
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 25 total points
ID: 34107021
try this:

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

Accepted Solution

by:
CEHJ earned 25 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
type mismatch (Object[] to double[] 4 33
ejb stateless example 2 18
mysql jsp example issue 32 48
Adding multiple JVM environments to RedHat 6 7 45
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 …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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 …

789 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