Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Regular expression handling

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Thanked 0 Times in 0 Posts

    Default Regular expression handling

    Regexes, to me, are a different language entirely, but I think I have the hang of it now. I'm writing a macro to convert a Word doc saved as .txt to a certain format which I can make more use of. I'm merely reading the file, concatenating it into one humongous string with newlines intact, and doing the following with this array:

    Entries 1 and 2 are the find/replace regexes, respectively.
    Entry 3 determines whether to run the replaceFirst(1,2) or replaceAll(1,2) method. This saved time originally, but now the two marked with * are taking up to 40 seconds to run, compared with <10 ms for the others.

    String[][] regexes = {
            {"(?i)half ?time","","a"},
            {"\n([0-9]{1,2}|(TIE|TB))(\\.|\\))? *","","a"},
            {"(?i)(.*\n)+\n*.*toss-?ups\n*","","f"}, // *
            {"(\n *)+","\n","a"},
            {"(?i)((.+\n?ANSWER:.+\n)*)(.*\n)+.*bon(uses|i)\n","$1‡ß","f"}, // *
            // more stuff

    I originally thought the drastically lengthened search time was because it was looking through the rest of the files, which aren't large at all, but replaceFirst() fires normally, so it's a problem with the actual expression. How do I fix this so that it runs in a time comparable to the others?

  2. #2
    Junior Member
    Join Date
    Apr 2012
    Thanked 0 Times in 0 Posts

    Default Re: Regular expression handling

    Pardon my french, but that thing is a clusterf**k.

    Why is it in a 2d String array?

    And the first regex: {"\t+" , "" , "a"}
    And the second: {"<.*?>\n*" , "\n","a"}

    Having your regexes separated like that and ultimately storing them in a 2d string array is confusing and silly.

    String replace1 = "";
    String regex1 = "[\ta]"

    text.replaceAll( regex, replace );

    Jesus I'm not going to attempt to decipher that mess. How the フck could anyone understand anything of what you're trying to do with and how you're doing it and why it doesn't work by pasting a long ass dustercluck of a regex like that?

    Read the Java Regex Tutorial again. And if you haven't already...
    Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Missouri, United States
    Thanked 2 Times in 2 Posts

    Default Re: Regular expression handling

    As mobydickus said, it is difficult to decipher what that code you posted actually does. Would you be able to provide a SSCEE or at least the entire java code for that regex class?

Similar Threads

  1. Regular Expression To Catch Duplicates
    By daniel_el in forum Java Theory & Questions
    Replies: 1
    Last Post: February 7th, 2012, 12:06 PM
  2. Replies: 3
    Last Post: September 30th, 2011, 08:45 AM
  3. How to remove the regular expression from a string ?
    By srimanta in forum Java Theory & Questions
    Replies: 5
    Last Post: July 20th, 2011, 03:07 PM
  4. Regular Expression help
    By medoos in forum Java SE APIs
    Replies: 0
    Last Post: March 19th, 2011, 07:23 PM
  5. Simple Hashsets and regular expression problem !
    By burningflower in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 10th, 2011, 01:43 PM