Solved

Java regex needed

Posted on 2011-09-28
10
171 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
[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
  • 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
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 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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 video teaches viewers about errors in exception handling.

730 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