Link to home
Start Free TrialLog in
Avatar of chocobogo
chocobogo

asked on

Searching a string for a paticular pattern.

I am writing a method that checked a passed string for a number of patterns, which if detected returns false..

The first method work fine, which checks the start of the string, but when I need to search within the string it fails.


                //This works
            //we dont want email addresses so look for email links:
            if(link.startsWith("<a href=\"mailto:"))
            {
                  return false;
                  //this recognises email addresses:
            }//end if



//This method doesn't work
            if(link.indexOf("related-author-search") != -1)
            {
                  return false;

            }

So the second method tries to detect whether the pattern "related-author-search" appears in the passed string, but my program never detects this.

Can anyone offer any insight
Avatar of apparition
apparition

Please post more code
Avatar of chocobogo

ASKER

     private boolean isValidLink(String link)
      {
            //wether it is a link we want or not:

            //we dont want email addresses so look for email links:
            if(link.startsWith("<a href=\"mailto:"))
            {
                  return false;
                  //this recognises email addresses:


            }//end if

            //we dont want external addresses so make sure starts with base:
            if(! link.startsWith("<a href=\"http://www.billybob/"))
            {
                  return false;

            }//end if

            //need to work out a way to make sure we are not going back up the tree:
            if(link.length() < this.thisFilesURL.length() )
            {
                  //i assume that if the length of the link must be greater
                  //  than or equal to that of its parent for it to be
                  //  continuning down that branch of the tree,
                  //  otherwise i assume it is going to an unrelated page?:
                  return false;

            }//end if

            //make sure we do not follow individual author links, typical one is:

            if(link.indexOf("related-author-search") != -1)
            {
                  return false;

            }//end if
            
            if(link.indexOf("http:\\www.jillybob") != -1)
            {
                  return false;
            }
            
            if(link.indexOf("img src=") != -1)
            {
                  return false;
            }//end if


            return true;

      }//end removeInvalidLinks()
ASKER CERTIFIED SOLUTION
Avatar of apparition
apparition

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
SOLUTION
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
SOLUTION
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
SOLUTION
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
SOLUTION
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
I suggest to split the points : apparition, SergeiKo, JugglerW, CodingExperts, Webstorm