Link to home
Start Free TrialLog in
Avatar of wsyy
wsyy

asked on

Java regex needed

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

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Open in new window

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

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

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

ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wsyy
wsyy

ASKER

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.
.+ this maens any charcter any tnumber of times but at least once
$ - means the end of the string
So it looks for the slash and then picks up anything after the slash to th end of the string
and this is called positive lookbehind (?<=/) - it checks that ther is a slash before , but does not include it into match
Avatar of wsyy

ASKER

Many thanks!