Solved

Searching a string for a paticular pattern.

Posted on 2004-09-17
11
167 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now