Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to capture a very flexible pattern in Java using regular expressions?

Posted on 2011-10-17
9
Medium Priority
?
182 Views
Last Modified: 2012-05-12
Hi,
I have the following line of code;

static Pattern ReportURL = 
Pattern.compile("<a.*?href=['\"](http://.*?['\"]).*?>.*mytests.*");

Open in new window


This captures the pattern and the string "mytests" after ">".

However, I wanna capture "mytests" if there is any anywhere in this whole pattern.

So the first regular expression can capture this:

-CJ "<a href='http://www-internal/sandbox/tolgar/Verify_mytests/glnxa64/mytest
/myscanlog_results.html'>mytests logs</a>"

Open in new window


Because, it has mytests after ">"

However it does not capture this:

-CJ "<a href='http://www-internal/sandbox/tolgar/Verify_mytests/glnxa64/mytest
/myscanlog_results.html'>logs</a>"

Open in new window


Because, mytests is in between somewhere in the whole text. And we cannot detect it.

How can I change this regular expression in Java to capture the both cases?

Thanks,
0
Comment
Question by:Tolgar
9 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36980788
So it looks likee you want to find all those which have "mystests"  in any place?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36980797
I meant  "mytests" of course
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36980807
Or there are still some limitations?
I guess ".*mytests.*" should match all occurrences of mytests - ?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 41

Expert Comment

by:HonorGod
ID: 36980809
When you say "capture", what, exactly do you mean?

Are you saying that your test of the RegExp using the specified text fails?

Are you are looking to detect the value of the href attribute when the anchor text includes "mytest"?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 36980885
Try something like
Pattern ReportURL = Pattern.compile("<a.*?href=['\"](http://.*?(?:mytests)*.*?)['\"].*?>.*(?:mytests)*.*");

Open in new window

0
 

Author Comment

by:Tolgar
ID: 36980939
Hi,
I need to give more information at this point.

I read a text file. So capturing .*mytests.* will not help me a lot. So, I need to define a regexp which will capture the two examples I gave you in the initial question.

So, href, http, any kind of slashes, <a, >, " should be part of the regexp.


This is the original question on this subject: ID: 35371138


But the criteria changed a little bit. Now "myruntests" can be anywhere in the string.


Thanks,


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36981240
>>But the criteria changed a little bit. Now "myruntests" can be anywhere in the string.

Just change 'mytests' in what i gave you to 'myruntests'
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36981454
If you justneed myruntests within links, then why do you need all kinds of slaahses,
this pattern I think worked for me:

                Pattern reportURL =
Pattern.compile(".*a href=.*myruntests.*");

        String s50 = "-CJ \"<a href='http://www-internal/sandbox/tolgar/Verify_myruntests/glnxa64/mytest/myscanlog_results.html'>mytests logs</a>\"";

        String s51 =   "-CJ \"<a href='http://www-internal/sandbox/tolgar/Verify_myruntests/glnxa64/mytest/myscanlog_results.html'>logs</a>\"";
        Matcher m50 = reportURL.matcher(s50);

           Matcher m51 =   reportURL.matcher(s51);


        while(m50.find()){

            System.out.println("s50: " + s50.substring(m50.start(),m50.end()));
        }



            while(m51.find()){

            System.out.println("s51;" + s51.substring(m51.start(),m51.end()));
        }

Open in new window


Output:
s50: -CJ "<a href='http://www-internal/sandbox/tolgar/Verify_myruntests/glnxa64/mytest/myscanlog_results.html'>mytests logs</a>"
s51;-CJ "<a href='http://www-internal/sandbox/tolgar/Verify_myruntests/glnxa64/mytest/myscanlog_results.html'>logs</a>"

Open in new window


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36982493
:)
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

564 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