Re: Regular Expression - Part II

Tom K <[email protected]>
Newsgroups gmane.comp.java.sun.servlet
Message-ID <002201c32b1b$153e1f50$6501a8c0@developer>
What I would do is make a short test program to test your expressions.
Here is one I found somewhere.

import java.io.*;
import java.util.regex.*;

public final class RegexTestHarness {

    private static String REGEX;
    private static String INPUT;
    private static BufferedReader br;
    private static Pattern pattern;
    private static Matcher matcher;
    private static boolean found;

    public static void main(String[] argv) {
        initResources();
        processTest();
        closeResources();
    }

    private static void initResources() {
       try {
           br = new BufferedReader(new FileReader("regex.txt"));
       }
       catch (FileNotFoundException fnfe) {
            System.out.println("Cannot locate input file!
"+fnfe.getMessage());
            System.exit(0);
        }
       try {
           REGEX = br.readLine();
           INPUT = br.readLine();
       } catch (IOException ioe) {}

        pattern = Pattern.compile(REGEX);
        matcher = pattern.matcher(INPUT);

        System.out.println("Current REGEX is: "+REGEX);
        System.out.println("Current INPUT is: "+INPUT);
    }

    private static void processTest() {
        while(matcher.find()) {
            System.out.println("I found the text \"" + matcher.group() +
                               "\" starting at index " + matcher.start()
+
                               " and ending at index " + matcher.end() +
".");
            found = true;
        }

        if(!found){
            System.out.println("No match found.");
        }
    }

    private static void closeResources() {
       try{
           br.close();
       }catch(IOException ioe){}
    }
}

Try these web sites for examples:
http://java.sun.com/docs/books/tutorial/extra/regex/quant.html
// The link below has some useful test code.
http://www.idg.net/english/crd_java_1145474.html
http://java.sun.com/j2se/1.4.1/docs/api/java/util/regex/Pattern.html
http://www.regexlib.com/Search.aspx
http://javaalmanac.com/egs/java.util.regex/pkg.html
http://www.linuxjournal.com/article.php?sid=5916


Tom Kochanowicz




-----Original Message-----
From: A mailing list for discussion about Sun Microsystem's Java Servlet
API Technology. [mailto:[email protected]] On Behalf Of
karthikeyan.balasubramanian
Sent: Wednesday, June 04, 2003 1:37 PM
To: [email protected]
Subject: Regular Expression - Part II

Hi guys,

I am using this package.  Hopefully people might know that i asked this
doubt few post before.
I made some start but just couldnt finish it.

http://jregex.sourceforge.net/gstarted.html#searching

  Help me here.

  I m trying out regular expression which can do something like this in
java.

<input type='checkbox' name='8' value='505'>some dynamic text MATCHING
TEXT</input>

and replaces this with

<input type='checkbox' name='8' value='505' DISABLED>some dynamic text
MATCHING TEXT</input>

I basically want something like this in a string for pattern searching:

String str1 = "<input type='checkbox' name='8' value='505' ";
String str2 = ">some dynamic text";
String str3 = "MATCHING TEXT";
String str4="</input>";

Pattern p= new Pattern(str1  str2  str3  str4;);
Replacer r=p.replacer("[$1, $2, $3 DISABLED, $4]");
prodInfo ="<input type='checkbox' name='8' value='505'>some dynamic text
MATCHING TEXT</input>";
String result=r.replace(prodInfo);

I m not very good at regular expression, so i basically know the steps
but
just couldnt get the right expression
to pass it on to Pattern class.

Any help would be appreciated.

<snip>
I tried this but cant go beyond this.
(?ism)<(\w+ \w+)=(\'\w+\') (\w+)=(\'\w+\') (\w+)=(\'\w+\')>(?s)\w+
</snip>

Looking forward for some response.

Have a great day.

Karthikeyan.

________________________________________________________________________
___
To unsubscribe, send email to [email protected] and include in the
body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.