简体   繁体   中英

Pattern match every occurence of first and third words (with everything inbetween)

I am trying to what I think is a simple regex pattern.

I have a text file that looks something like this:

Blah Blah Blah http://www.google.com . Something else here, blah blah bah. http://x.oddree.com/image1.jpg . Some more text ... blah blah. http://x.oddree.com/image2.jpg

What I am trying to grab out of there, is any text that begins with http and ends with jpg. Not entire urls. Only the ones ending in .jpg.

In other words, I want my output to be:

http://x.oddree.com/image1.jpg http://x.oddree.com/image2.jpg

My regex would be: "\\b(http).*(jpg)\\b" and it seems to work. But when used with a pattern match, I end up getting everything from the first occurrence of http to the last occurrence of jpg. I know that I have to double-escape changing \\b to \\b, but it still doesn't work as expected.

I have been banging my head against the wall for hours on this. :-)

Here is a code snippet:

             if(file.exists())
             {
                 StringBuilder text = new StringBuilder();

                 try {
                     BufferedReader br = new BufferedReader(new FileReader(file));
                     String line;

                     while ((line = br.readLine()) != null) {
                         text.append(line);
                         text.append('n');
                     }
                 }
                 catch (IOException e) {
                     //TODO Write some error handling.
                 }

                 Pattern pattern = Pattern.compile(
                         "\\b(http).*(jpg)\\b"
                     );

                 Matcher matcher = pattern.matcher(text);
                 while (matcher.find()) {

                     result.add(matcher.group());
                 }

                 my_text.setText(result.toString());
             }

尝试

Matcher m = Pattern.compile("\\bhttp://\\S+?\\.jpg\\b").matcher(s);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM