Solved

Java regex needed

Posted on 2011-09-28
10
170 Views
Last Modified: 2012-05-12
Hi,

How can I use the attached code to extract "B004UJLUQU" from "dp/B004UJLUQU"?

The attached code returns the first matched string, and has been very helpful in the past such that I don't want to change it.

Thanks
public static String extractRegex(String source, String regex){
		Pattern re = Pattern.compile(regex, Pattern.DOTALL|Pattern.MULTILINE|Pattern.CASE_INSENSITIVE);
		Matcher m = re.matcher(source);
		while (m.find())
			return m.group(0);
		return null;
	}

Open in new window

0
Comment
Question by:wsyy
  • 7
  • 2
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 36717956
Something like the following should work
public static String extract(String s) {
        return s.replaceAll(".*?/(.*)", "$1");
    }

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36717958
Why wouldnot you use "B004UJLUQU" as the pattern:

public static String extractRegex(String source, String regex){
		Pattern re = Pattern.compile("B004UJLUQU", Pattern.DOTALL|Pattern.MULTILINE|Pattern.CASE_INSENSITIVE);
		Matcher m = re.matcher(source);
		while (m.find())
			return m.group(0);
		return null;
	}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36718013
I tested this:

                 String source = "dp/B004UJLUQU";

//public static String extractRegex(String source, String regex){
		Pattern re = Pattern.compile("/(.+)$", Pattern.DOTALL|Pattern.MULTILINE|Pattern.CASE_INSENSITIVE);
		Matcher m = re.matcher(source);
		while (m.find())
			System.out.println(m.group(1));
	//	return null;

Open in new window


Output:
B004UJLUQU

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
LVL 47

Expert Comment

by:for_yan
ID: 36718038
This is as a method, though I changed group(0) to group(1):
  public static String extractRegex(String source, String regex){
		Pattern re = Pattern.compile(regex, Pattern.DOTALL|Pattern.MULTILINE|Pattern.CASE_INSENSITIVE);
		Matcher m = re.matcher(source);
		while (m.find())
			return m.group(1);
		return null;
	}


		   public static void main(String[] args){





                 String source = "dp/B004UJLUQU";
                  String regex = "/(.+)$";
           System.out.println(extractRegex( source, regex));

}

Open in new window


Output:

B004UJLUQU

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 50 total points
ID: 36718094
Now if you want to keep the method intact (with group(0)) that's how you do it:
    public static String extractRegex(String source, String regex){
		Pattern re = Pattern.compile(regex, Pattern.DOTALL|Pattern.MULTILINE|Pattern.CASE_INSENSITIVE);
		Matcher m = re.matcher(source);
		while (m.find())
			return m.group(0);
		return null;
	}


          String source = "dp/B004UJLUQU";
                  String regex = "(?<=/).+$";

               System.out.println(extractRegex( source, regex));

Open in new window


B004UJLUQU

Open in new window


0
 

Author Comment

by:wsyy
ID: 36718229
CEH, I don't want to change group(0) in the method.

for_yan, the last method you provided works. can you please explain it a little bit?

I guess that (?<=/) removes the "/", but I don't understand the ".+$" part.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36718233
.+ this maens any charcter any tnumber of times but at least once
$ - means the end of the string
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36718245
So it looks for the slash and then picks up anything after the slash to th end of the string
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36718256
and this is called positive lookbehind (?<=/) - it checks that ther is a slash before , but does not include it into match
0
 

Author Closing Comment

by:wsyy
ID: 36718335
Many thanks!
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
REReplaceNoCase help 1 43
tomcat startup error 5 104
how to install java on RHEL image on EC2 4 41
parse a file and get data out 11 78
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

829 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