Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Searching a string for a paticular pattern.

Posted on 2004-09-17
11
Medium Priority
?
177 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 200 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
Technology Partners: 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!

 
LVL 2

Assisted Solution

by:SergeiKo
SergeiKo earned 200 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 200 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 200 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 200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

971 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