Regular expressions API

Hello every body. My question is simple. Can I use regular expresions API in JDK 1.3?
Thanks in advance :)

interesting idea.
I do not believe you can use 1.4's regular expression package because i think it uses a bunch of other apis that are also new to 1.4 like channels and nio but i could be very wrong about that.
however, you can upgrade your java version or simply get the open source package .
Stephen

Similar Messages

  • Regular expressions API in v1.3.1

    hi,
    is there any package like java.util.regex of java version 1.4.1 in java version 1.3.1? is it possible to use regular expressions in 1.3.1 version? i need methods like matches() and replaceAll() in Matcher class of version 1.4.1
    if not, is there any external packages for this purpose?

    You can use ORO from Apache Jakarta Project.
    "The Jakarta-ORO Java classes are a set of text-processing Java classes that provide Perl5 compatible regular expressions, AWK-like regular expressions, glob expressions, and utility classes for performing substitutions, splits, filtering filenames, etc. "
    URL: http://jakarta.apache.org/oro/index.html

  • Regular Expressions and String variables

    Hi,
    I am attempting to implement a system for searching text files for regular expression matches (similar to something like TextPad, etc.).
    Looking at the regular expression API, it appears that you can only match using string variables. I just wanted to make sure this is true. Some of these files might be large and I feel uneasy about loading them into ginormous Strings. Is this the only way to do it? Can I make a String as big as I want?
    Thanks,
    -Mike

    Newlines are only a problem if you're reading the
    text line-by-line and applying the regexp to each
    line. It wouldn't catch expressions that span
    lines.
    @sabre150: your note re: CharSequence -- so what
    you're suggesting is to implement a CharSequence that
    wraps the file contents, and then use the regexps on
    the whole thing? I like the idea but it seems like
    it would only be easy to implement if the file uses a
    fixed-width character set. Or am I missing
    something...?You are correct for the most basic implementation. It is very easy to create a char sequence for fixed width character sets using RandomAccessFile. Once you go to character sets such as UTF-8 then more effort is required.
    While ever the regex is moving forward thought the CharSequence one char at a time there is no problem because one can wrap a Reader but once it backtracks then one needs random access and one will need to have a buffer. I have used a ring buffer for this which seems to work OK but of course this will not allow the regex to move to any point in the CharSequence.
    'uncle_alice' is the regex king round here so listen to him.
    :-( I should read further ahead next time!
    Message was edited by:
    sabre150
    Message was edited by:
    sabre150

  • ANN: New Regular Expressions Sample

    Hi,
    Regular expressions introduced in Oracle 10g Database is one of the most powerful ways of searching, manipulating and transforming text data.
    Check out this new [url http://www.oracle.com/technology/sample_code/tech/pl_sql/regexp/dnasample/readme.html]DNA Analysis Sample, that uses the regular expression APIs to identify certain enzyme patterns in a DNA Sequence.
    Also visit the [url http://www.oracle.com/technology/sample_code/tech/pl_sql/index.html]PL/SQL Sample Code page for more samples on PL/SQL.
    Thanks
    Shrinivas
    OTN Team

    This bunch of samples does not currently have one. We will look at it.
    Cheers,
    -- Rajesh

  • Regular expressions: serious design flaw?

    I wondered why sometimes, the replaceAll() method works in my code and sometimes it throws a java.util.regex.PatternSyntaxException. I wrote a little test case to show the problem
    private void regularExpressionTest() {
            String escapers = "\\([{^$|)?*+."; //characters to escape to avoid PatternSyntaxException
            String text = "The quick brown fox jumps over the lazy dog.";
            System.out.println("Original: "+text);
            String regExp = "o";
            String word = "HELLO";
            String result = text.replaceAll(regExp, word);
            System.out.println("Replace all '"+regExp+"' with "+word+": "+result);
            text = "The quick brown {animal} jumps over the lazy dog.";
            System.out.println("Original: "+text);
            regExp = "{animal}";
    //        regExp = escapeChars(regExp, escapers); //escape possible regular expression characters
            word = "catterpillar";
            result = text.replaceAll(regExp, word);
            System.out.println("Replace all '"+regExp+"' with "+word+": "+result);
    }//regularExpressionTest()The output is:
    Original: The quick brown fox jumps over the lazy dog.
    Replace all 'o' with HELLO: The quick brHELLOwn fHELLOx jumps HELLOver the lazy dHELLOg.
    Original: The quick brown {animal} jumps over the lazy dog.
    java.util.regex.PatternSyntaxException: Illegal repetition
    {animal}
         at java.util.regex.Pattern.error(Pattern.java:1528)
         at java.util.regex.Pattern.closure(Pattern.java:2545)
         at java.util.regex.Pattern.sequence(Pattern.java:1656)
         at java.util.regex.Pattern.expr(Pattern.java:1545)
         at java.util.regex.Pattern.compile(Pattern.java:1279)
         at java.util.regex.Pattern.<init>(Pattern.java:1035)
         at java.util.regex.Pattern.compile(Pattern.java:779)
         at java.lang.String.replaceAll(String.java:1663)
         at de.icomps.prototypes.Test.regularExpressionTest(Test.java:57)
         at de.icomps.prototypes.Test.main(Test.java:34)
    Exception in thread "main" As the first replacement works as espected, the second throws an Exception. Possible because '{' is a special character for the regular expression API. In case I know the regular expression, i could escape these special characters. But in my generic server app, the strings are all parameters, so the only way for replaceAll() to work is, to escape all possible special characters in the regular expression.
    1. Is there a complete list of all special characters for regular expressions that need to be escaped?
    2. Is there a similar replaceAll() method without regular expressions? So that all occurences of a substring can be replaced by another string? (So far, I wrote my own method but this is of course more time consuming for massive string operations.)

    1. The complete list of specially-recognized characters are listed in the Java 1.4.* API. (Of course, new ones may eventually be added as the regex package matures).
    2. It is time consuming to program in general. You should have written your own utility method that goes through a String using indexOf and building up the String in a StringBuffer, and apparently you did. Now you have the utility method...you no longer need to write that method again.
    3. Or you could have written some kind of method that automatically escapes the specially-recognized characters...

  • Spliting a large string using regular expression which contain special char

    I have huge sting(xml) containing normal character a-z,A-Z and 0-9 as well as special char( <,>,?,&,',",;,/ etc.)
    I need to split this sting where it ends with </document>
    for e.g.
    Original String:
    <document>
    <item>sdf</item>
    <item><text>sd</text</item>
    </document>
    <document>hi</document>
    The above sting has to be splited in to two parts since it is having two document tag.
    Can any body help me to resolve this issue. I can use StringTokenizer,String split method or Regular expression api too.

    manas589 wrote:
    I used DOM and sax parser and got few exception. Again i don't have right to change xml. so i thought to go with RegularExpression or some other way where i can do my job.If the file actually comes in lines like what you posted, you should just be able to compare the contents of each line to see if it contains "</document>" or whatever you're looking for. I wouldn't use regex unless I needed another problem.
    I got excpetion like: Caused by: org.xml.sax.SAXParseException: The entity "nbsp" was referenced, but not declared.
         at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
         at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
         at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)So then it isn't even XML.
    Edit: sorry, I just realized why you're considering all of these heavy-duty ideas. It's just that you don't know how to break the string into lines. You do it like this:
    BufferedReader  br = new BufferedReader(new StringReader(theNotXMLString));

  • Java regular expression for CSV?

    I found several regular expressions in the internet to parse/split csv data lines. Howeverm, they all don't work with the Java regular expression API. Is there a regular expression to tokenize CSV fields for the Java regexp API?

    If the licensing of the above solution is too restrictive for you...I'm sure there are other types of parsers out there that do that type of thing.
    In the meantime, here is some code I cooked up (no GPL...use it freely) that might get you started.
    Don't know that it handles everything, but I never said it would...
    Please READ and let me know what changes could be made. I'm always looking for improvements in my understanding of regular expressions...
    import java.util.regex.*;
    import java.util.*;
    import java.util.List;
    public class Example
       final static Pattern CSV_PATTERN;
       final static Pattern DOUBLE_QUOTE;
       static
          String regex = "(?:  ([^q;]+) | (?: q ((?: (?:[^q]+) | (?:qq) )+ ) q)  );?";
          //                       1          2          a           b       3    4
          // So, pretend your quote character is q
          // (you can change it to \" later when you understand what's going on.)
          // This regex (when applied iteravely) matches a token that:
          // 1) contains NO QUOTE MARKS whatsoever (;'s) (in group 1)
          //                       or
          // 2) starts with a QUOTE, then contains either
          //    a) no quotes at all inside or
          //    b) double quotes (to escape a quote)
          // 3) and ends with a QUOTE.
          // 4) and is followed by a separator (optional for the last value)
          // Note that (a) and (b) are captured in group 2 of the regex.
          CSV_PATTERN = Pattern.compile(regex, Pattern.COMMENTS);
          DOUBLE_QUOTE = Pattern.compile("qq");
        * Attempts to parse Excel CSV stuff...
        * @param text the CSV text.
        * @return a list of tokens.
       public static List parseCsv(String text)
          Matcher csvMatcher = CSV_PATTERN.matcher(text);
          Matcher doubleQuotes = DOUBLE_QUOTE.matcher("");
          List list = new ArrayList();
          while (csvMatcher.find())
             if (csvMatcher.group(1) != null)
                // The first one matched.
                list.add(csvMatcher.group(1));
             else
                doubleQuotes.reset(csvMatcher.group(2));
                list.add(doubleQuotes.replaceAll("q"));
          return list;
    }

  • Problem in creating a Regular Expression with gnu

    Hi All,
    iam trying to create a regular expression using gnu package api..
    gnu.regex.RE;
    i need to validate the browser's(MSIE) userAgent through my regular expression
    userAgent is like :First one ==> Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
    i wrote an regular expression like this:
    Mozilla.*(.*)\\s*(.*)compatible;\\s*MSIE(.*)\\s*(.*)([0-9]\\.[0-9])(.*);\\s*(.*)Windows(.*)\\s*NT(.*)\\s*5.0(.*)
    Actaully this is validating my userAgent and returns true, my problem is, it is returning true if userAgent is having more words at the end after Windows NT 5.0 like Second One ==> Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) Testing
    i want the regularExpression pattern to validate the First one and return true for it, and has to return false for the Second one..
    my code is:
    import gnu.regexp.*;
    import gnu.regexp.REException;
    public class TestRegexp
    public static boolean getUserAgentDetails(String userAgent)
         boolean isvalid = false;
         RE regexp = new RE("Mozilla.*(.*)\\s*(.*)compatible;\\s*MSIE(.*)\\s*(.*)([0-9]\\.[0-9])(.*);\\s*(.*)Windows(.*)\\s*NT(.*)\\s*5.0(.*)");
         isvalid = regexp.isMatch(userAgent);
         return isvalid;
    public static void main(String a[])
         String userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
         boolean regoutput = getUserAgentDetails(userAgent);
         System.out.println("***** regoutput is ****** " + regoutput);
    }please help me in solving this..
    Thanks in Advance..
    thanx,
    krishna

    Ofcourse, i can do comparision with simple string matching..
    but problem is the userAgent that i want to support is for all the MSIE versions ranging from 5.0 onwards, so there will the version difference of IE like MSIE 6.0..! or MSIE 5.5 some thing like that..
    any ways i will try with StringTokenizer once..!
    seems that will do my work..
    Thanks,
    krishna

  • Regular Expression Question

    Hi all,
    I am suffering in java regular expression, and I hope you guys can help me out. I want to use the String api ".matches" to find out any string pattern like "xxxx.xxxx" where xxx can be only english word(both upper and lower case). Actually I will use this kind of expression to represent the cross join SQL statement in my java class, like "tableA.name = tableB.name", where they should be english letter only. I tried to use MyString.matches("^[A-Z] + \\. + ^[A-Z]") in my java program, but seem it doesn't work. Can you guys figure out the right expression for me ?? Many thanks
    Transistor

    Thanks for your prompt response, I tried your code, however, it doesn't work out.
    I put your code like the following:
    if ( searchCriteria.getStringPair().getValue().trim().matches("[A-Za-z]+\\.[A-Za-z]+") {...some action }.
    Seems the java program never reach this expression.
    Kindly remind that I wan to expression anything like "xxxxx.xxxxx" where xxxx can be a word.
    Myriads of thanks
    Transistor

  • Help!!!!! Regular Expressions!!

    I am trying to use Regular Expressions, for parsing. For that the pakage required is
    java.util.regex.*;
    I am also using the import statement in a sample code. But compiling it, gives an error,
    ERRORS:
    Replacement.java:6: package java.util.regex does not exist
    import java.util.regex.*;
    ^
    I have also set the path to C:\jdk1.4\bin
    I have also set the classpath to C:\jdk1.4\lib
    I don't know, Why it doesn't recognise the java.util.regex package
    please help!!
    gaurav_k1

    Have you checked if the regex package is part of the
    JDK1.4? I can't find it. What classes does it
    implement?Yeah, since 1.4
    http://java.sun.com/j2se/1.4/docs/api/java/util/regex/package-summary.html
    I'm not sure what the original problem could be, possibly using a previously installed jre? If you had one previously installed, check the classpaths and uninstall any old jre (some forget that thinking they only need to remove the jdk). Could you give us anymore hints?

  • Regular expressions in Workshop 8.1

    Hello,
    I'm posting this question here because I don't see a "jdk" subcategory in this
    newsgroup and it might be problem peculiar to Workshop.
    I'm trying to use the Pattern and Matcher classes in java.util.regex (JDK 1.4.2)
    in BEA Workshop 8.1, but I'm getting "ERROR: Unknown escape code" (red squiggly
    line appears under the regex and this message is the screen tip) whenever I try
    to use the backslash to escape a special character in the Pattern.compile() and
    the Pattern.matches() methods.
    For example, it doesn't allow "\d" to mean "any digit". For this particular one,
    I can get around the problem by specifying "[0-9]", but in the case of the period
    character, I'm stuck. I cannot use "\." However, the JDK API doc (http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html)
    says the backslash is to be used for this purpose, if I'm reading it correctly.
    Is this a problem with Workshop, and is there a workaround? I need to specify
    that I require one and exactly one period.
    Any help would be most appreciated!
    Thanks.

    Yes, I had read the Java doc, but I guess I hadn't fully understood it. Now I
    do! Thanks!!
    David
    Josh Eckels <[email protected]> wrote:
    This isn't particular to Workshop, but you'll need to use two
    backslashes in your source code. Inside a string, backslash is used to
    escape the next character so that you can enter special characters like
    newlines ('\n'), tabs ('\t'), etc.
    So, in order to enter a backslash character into your string, you need
    to escape it, like '\\'.
    There's a small section on this in the java.util.regex.Pattern JavaDoc,
    under the "Backslashes, escapes, and quoting" header:
    Backslashes within string literals in Java source code are interpreted
    as required by the Java Language Specification as either Unicode escapes
    or other character escapes. It is therefore necessary to double
    backslashes in string literals that represent regular expressions to
    protect them from interpretation by the Java bytecode compiler. The
    string literal "\b", for example, matches a single backspace character
    when interpreted as a regular expression, while "\\b" matches a word
    boundary. The string literal "\(hello\)" is illegal and leads to a
    compile-time error; in order to match the string (hello) the string
    literal "\\(hello\\)" must be used.
    Josh
    David Chang wrote:
    Hello,
    I'm posting this question here because I don't see a "jdk" subcategoryin this
    newsgroup and it might be problem peculiar to Workshop.
    I'm trying to use the Pattern and Matcher classes in java.util.regex(JDK 1.4.2)
    in BEA Workshop 8.1, but I'm getting "ERROR: Unknown escape code" (redsquiggly
    line appears under the regex and this message is the screen tip) wheneverI try
    to use the backslash to escape a special character in the Pattern.compile()and
    the Pattern.matches() methods.
    For example, it doesn't allow "\d" to mean "any digit". For this particularone,
    I can get around the problem by specifying "[0-9]", but in the caseof the period
    character, I'm stuck. I cannot use "\." However, the JDK API doc
    (http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html)
    says the backslash is to be used for this purpose, if I'm reading itcorrectly.
    Is this a problem with Workshop, and is there a workaround? I needto specify
    that I require one and exactly one period.
    Any help would be most appreciated!
    Thanks.

  • Regular expressions and back references

    Just wanted to know if anyone else noticed that.
    In the javadoc of java.util.regex.Pattern in the "Back references" section it says that you need to use \n to match capturing group but it does not work. To match a capturing group one need to use a "$" sign which is not standard for this type of operation.
    For example, the following code should work according to the API and most other regular expression engines:
    Pattern.compile("([A-Z])").matcher("ThisIsATestString").replaceAll(" \1");
    But to make this work you need to use:
    Pattern.compile("([A-Z])").matcher("ThisIsATestString").replaceAll(" $1");
    So, is this just a doc bug or am I missing something?
    Someone have any idea why Sun choose to use the "$" sign instead of the regular "\" sign??
    TIA,
    Shaul

    The doc you're referring to is talking about using back-refereneces within the regex, not in the replacement string. For instance, if you wanted to find all instances of things like "foo-foo" or "bar-bar", you would use a Pattern like   Pattern p = Pattern.compile("([a-z]+)-\\1");For the most part, they've made the syntax the same a Perl's regexes, and that's why they use $n instead of \n in the replacement string. The replacement string is described in the Matcher javadoc.

  • Help Finding Regular Expressions

    Does Java have regular expressions? Where can I find them in the API?
    Thanks!

    You can find them in the package java.util.regex (from version 1.4).
    If you don't have that version, there is a GNU regular expression package available.
    S&oslash;ren Bak

  • Pllllllease help!!!! Regular expressions

    Hi....i've been trying for almost 40 hours to write a regular expression and i don't succeed.......
    I need a regex that matches a polinomial number.
    that polinomal number "divided" in bracets with a complex number between them.
    the enviorment i'm using is java eclipse with the REGEX library .
    Example for a correct input:
    avi=(25.0+5.0i)x^2+(15.3+2.85i)x^1
    this is the regex i wrote
    ^[\w]+=[\\(]{1}[-+]?[\d]+[\\.]{1}[\d]++[+-]{1}[\d]+[\\.]{1}[0-9]+i]*[\\)]{1}[xX]{1}[\\^]{1}[\d]+$
    the regex has to start with a name than = than 1 bracet than possibly a + or - than a number with a decimal than + or minus than i than 1 bracket (closing bracket) than x or X than 1 "^" sign than atleast one number than i want the pattern e.g (15.3+2.85i)x^1
    the regex currently supports only this case : avi=(25.0+5.0i)x^2
    but i want it to support this: avi=(25.0+5.0i)x^2+(15.3+2.85i)x^1
    and the "+" sign between the two polinoms must be a "+" and not a "-"
    how do i define that the pattern will repeat it self more once or more - when i say the pattern I mean this one : (25.0+5.0i)x^2
    in conclusion. how do i fix it??
    plz plz plz help me i'm going nuts and me and java's api are close buddies after this weekend still i don't succeed...
    tnx alot in advance...........
    (:

    Arg! Why do you double-post???
    I've just taken considerable time in answering your other post ( http://forum.java.sun.com/thread.jspa?messageID=10018850 ) and then found out that you posted here as well with additional information.
    It's considered rude to make people duplicate their effort by posting the same question twice. Keep to one thread.

  • Can I use regular expressions in Java 1.3

    Hi,
    Dose Java 1.3 suport regular expressions?
    How can I use it?
    Thanks.
    bevin ye

    The 1.3 core API doesn't support regular expressions. Hint: There's an item "Since:" in the JavaDoc of most classes that indicates the version it was initially available. If you look it up in the JavaDoc of java.util.regex.Pattern you'll notice that it's value is 1.4.
    But there are several third party libraries that implement regular expressions, 'though I've not used them extensivly, so I can't tell you which one's the most usefull.

Maybe you are looking for

  • My home sharing is not working - connection is dropping out from iMac to iOS devices

    I have a 2011 iMac running Mountain Lion, and am having real problems accessing my itunes library with my iOS devices (I had the same problem with Apple TV before I bought a set of power line adapters and now running an Ethernet into it). I keep my i

  • Is my hard drive completely shot?

    I have had my IMac for 3 years and rarely used it... One day I turned it on and I got the dreaded "blinking folder with a question mark" screen. I have tried everything that all the support sections say to do before the section that walks you through

  • Where is my iWork serial number?

    I got an iWork trial with Tiger and after my trial ended I bought the full version electronically. Now I am left wondering where exactly my serial number is. I don't remember having an iWork booklet in my Tiger box and I am sure that I kept everythin

  • HTML Gallery language

    Hi, I need to change the HTML web gallery navigation "word" Next, Previous and Index to norwegian language. I see that the words get hardcoded into every html file. Where can I change these words - is there a language file or "important words" file?

  • Change the source address in socket

    Hi all, I need some help here. I need to write a program to forward the UDP message received to another machine. The requirement form my boss is that the source ip field must remain the same as when I receive it. However, when I forward the message,