Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 177
  • Last Modified:

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

0
wsyy
Asked:
wsyy
  • 7
  • 2
1 Solution
 
CEHJCommented:
Something like the following should work
public static String extract(String s) {
        return s.replaceAll(".*?/(.*)", "$1");
    }

Open in new window

0
 
for_yanCommented:
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
 
for_yanCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now