Solved

Searching a string for a paticular pattern.

Posted on 2004-09-17
11
170 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
reverse digits of a number using for loop 5 52
varialbe initialization 11 37
how to see all occupied ports on windows 10 laptop 15 77
How do I remove an object from a 3 40
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

828 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