Solved

Searching a string for a paticular pattern.

Posted on 2004-09-17
11
169 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:chocobogo
11 Comments
 
LVL 5

Expert Comment

by:apparition
ID: 12087594
Please post more code
0
 

Author Comment

by:chocobogo
ID: 12087656
     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()
0
 
LVL 5

Accepted Solution

by:
apparition earned 50 total points
ID: 12087699
Try

 private boolean isValidLink(String link)
     {
          //wether it is a link we want or not:
          boolean rtrnValue = true;
          //we dont want email addresses so look for email links:
          if(link.startsWith("<a href=\"mailto:"))
          {
               rtrnValue = false;
               //this recognises email addresses:


          }//end if

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

          }//end if

          //need to work out a way to make sure we are not going back up the tree:
          else 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?:
              rtrnValue = false;

          }//end if

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

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

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


          return rtrnValue;

     }//end removeInvalidLinks()
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 2

Assisted Solution

by:SergeiKo
SergeiKo earned 50 total points
ID: 12087907
Hello, chocobogo.

Could you provide the link String you are testing.


May be you need case insensitive search of substring?

Look at this snippet for some ideas:


class Searcher
{
    public static void main( String[] args )
    {
        String correctLink  = "bad link is related-author-search";
        String badLink      = "good link is Related-author-search";

        System.out.println( correctLink.indexOf( "related-author-search" ) );
        System.out.println( badLink.indexOf( "related-author-search" ) );
        System.out.println( badLink.toLowerCase().indexOf( "related-author-search" ) );
    }
}

Regards.
0
 
LVL 7

Assisted Solution

by:JugglerW
JugglerW earned 50 total points
ID: 12088179
Shouldn't it be:

          if(link.indexOf("related-author-search") < 0)
          {
               return false; // pattern not found in link
          }

because indexOf returns a number < 0 (normally -1) if the string is not found, but your method above returns false if the pattern is found in the string?
0
 
LVL 6

Assisted Solution

by:CodingExperts
CodingExperts earned 50 total points
ID: 12090583
This snippet possibly seems to work so should yours.... check out the descrepancy.
class test
{
  public static void main(String [] args)
  {
    String link = "<a href=\"mailto:";
    String newLink = " sdfsdf related-author-search sdfsdf ";
    boolean val = isValidLink(link);
    System.out.println("val = "+val);
    val = isValidLink(newLink);
    System.out.println("val newlink = "+val);
  }
  static boolean isValidLink(String link)
  {
    if(link.startsWith("<a href=\"mailto:"))
        return false;
    if(link.indexOf("related-author-search") != -1)
      return false;
    return true;
  }
}

bye and good luck
CodingExperts
0
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 50 total points
ID: 12101193
Hi chocobogo,

If uppercase and lowercase is not important, then convert the link to lowercase before testing it :

    private boolean isValidLink(String link)
     {
          //wether it is a link we want or not:
          link=link.toLowerCase(); // <---
          ...

0
 
LVL 13

Expert Comment

by:Webstorm
ID: 12408592
I suggest to split the points : apparition, SergeiKo, JugglerW, CodingExperts, Webstorm
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

805 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