Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Mini Seti application(regular expressions)

    Can I please have some help over here with a program,more the algorithm..I can't figure it out all the cases for this problem:

    "Mini Seti"
    The application has 2 components:
    Reception(server) wich generates a random list of string characters(>1000) and sends them for the processing to the client(processing units).
    Reciever(client) search in the list of characters a regular expression.
    The answer for the client would look like this(total number of apparitions,the context of the apparition.

    Example(in the ASCII code):
    Server sends the following code(characters from 48 to 122 in ASCII):
    ajd73jcl392mx30v;]3p2i4j59d9i3o23j4v0su345imsdf-f4-i35876hl2f

    The client for example searches for the expression ".[0-9]." which means anythingnumberanything. Answer should look like this:
    (number of aparitions,d73 73j l39 392 92m x30 30v ]3p p2i i4j j59 59d......and so on)

    My problem here is that I cannot view all of them because my find() function only gets the list once and doesnt show expressions like: d73 73j cause find function doesnt look back.

    Here is the part of the code I need to get it work(the client):
    Code:
    import java.net.*;
    import java.io.*;
    import java.util.regex.*;
    
    public class Client {
        
       public static void main(String[] args) throws IOException
       {
           String adr_server="127.0.0.1";
           int PORT=8111;
           Socket s1=null;
           PrintWriter out=null;
           BufferedReader in = null;
           String feedback=new String();
           
           try
           {
               s1 = new Socket(adr_server,PORT);
               out=new PrintWriter(s1.getOutputStream(),true);
               in=new BufferedReader(new InputStreamReader(s1.getInputStream()));
               
               feedback=in.readLine();
               System.out.println("Am primit de la server " +feedback);
               Pattern p = Pattern.compile( ".[0-9].");
               Matcher m = p.matcher(feedback);
                String rasp="";
               int i=0;
               
               while(m.find()) 
               {
                //System.out.println("am gasit   "+m.group());
                 rasp+=m.group()+" ";
                i++;
               } 
               System.out.println("Am gasit asemanari in numar de: " +i);
                
              
               
               out.println("("+i+","+rasp+")");
           }catch(UnknownHostException e)
           {
               System.err.println("Serverul nu poate fi gasit "+e);
               System.exit(1);
           }
           finally
           {
               if(out!=null)
                   out.close();
               if(in!=null)
                   in.close();
               if(s1!=null)
                   s1.close();
           }
       }
    }

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    drop characters from the beginning and repeat the matching iteratively till you find all matches and save only the unique ones...sorta like this

    Code:
    String s = "ajd73jcl392mx30v;]3p2i4j59d9i3o23j4v0su345imsdf-f4-i35876hl2f";
    HashSet<String> matches = new HashSet<String>();
    
    while ( s.length() > 3 /* match cannot succeed below this */ ) {
      Matcher m = Pattern.compile(".[0-9].").matcher(s);
      while ( m.find() ) {
        matches.add(m.group());
      }
      s = s.substring(1);
    }
    
    // now matches contains all the possible matches
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    New to the CF scene
    Join Date
    May 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hey..thanks for your reply...but the program is supposed to work with any regular expressions not only with the ones that have length of 3. Also encounters of the expression on the same index should only be printed out once..


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •