Solved

java pattern matcher find string before the matching

Posted on 2010-11-10
9
309 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 74

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 74

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 74

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

707 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

14 Experts available now in Live!

Get 1:1 Help Now