Solved

java pattern matcher find string before the matching

Posted on 2010-11-10
9
310 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 77
Date input validation (Regular Expressions) 1 39
jar file executable 12 44
Apps blocked by Java 9 64
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now