Solved

Regular Expressions and Strings

Posted on 2002-04-17
7
179 Views
Last Modified: 2010-08-05
Basically I have a program with two text edit boxes and a button on it.  The user can enter a piece of text into the first edit box and a regular expression into the second edit box.

When the user presses the button I want it to process the regular expression against the text that the user entered.  

Is there any way to implement this using Java ?

I would be grateful for all advice offered regarding this.

Thanks in advance

John Clarke
0
Comment
Question by:johnclarke
7 Comments
 
LVL 2

Expert Comment

by:Andrey_Kulik
ID: 6947287
Hi
in JDK 1.4  see package java.util.regex.*
in additional see:
http://jakarta.apache.org/regexp/index.html

0
 
LVL 2

Expert Comment

by:CSuvendra
ID: 6947294
Implementation of Regular Expressions under GNU license

http://www.cacas.org/java/gnu/regexp/
0
 
LVL 9

Accepted Solution

by:
Venci75 earned 50 total points
ID: 6947300
java 1.4 have classes for processing regular expressions

try this sample code:
import java.util.regex.*;
...
System.out.println("" + Pattern.compile("[0-9]").matcher("1").matches());
System.out.println("" + Pattern.compile("[0-9]").matcher("d").matches());
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:Moondancer
ID: 6956327
Please click the link below to manage your open and locked questions here to provide feedback and closures for those who have helped you.
http://www.experts-exchange.com/jsp/memberProfile.jsp?mbr=johnclarke&showQHistory=true&qAskBegin=11&qAskEnd=89#1
Thanks,
Moondancer - EE Moderator
0
 

Expert Comment

by:master_p
ID: 6968390
See http://jakarta.apache.org/regexp/index.html

import org.apache.oro.text.regex.*;

public class Tester
{
    public boolean matches(
        String aRegularExpression,
        String aString)
    throws Exception
    {
        Perl5Compiler myCompiler = new Perl5Compiler();
       
        Pattern myPattern = myCompiler.compile(
                    aRegularExpression,
                    Perl5Compiler.DEFAULT_MASK);

        PatternMatcher myMatcher = new Perl5Matcher();

        return myMatcher.matches(aString, myPattern);
    }

    public static void main(String anArg[])
    throws Exception
    {
        Tester myTester = new Tester();
        System.out.println(myTester.matches(anArg[0], anArg[1]));
    }
}
0
 

Author Comment

by:johnclarke
ID: 6970158
Although it is a good answer I prefer to use the native classes provided with JDK.  Sorry.

John
0
 

Author Comment

by:johnclarke
ID: 6970159
Thanks for pointing me to where I wanted to go.

John
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

830 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