Solved

Java regex needed

Posted on 2011-09-28
10
166 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 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

11 Experts available now in Live!

Get 1:1 Help Now