Solved

Searching a string for a paticular pattern.

Posted on 2004-09-17
11
171 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 113
JAVA API design with micro service cloud in mind 1 115
How to configure empty element in XML Document parser? 15 59
junit initializtion error 2 22
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

752 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