Results 1 to 3 of 3
  1. #1
    radgator is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default Regular Expression Help Needed

    I have a string that i am trying to return from a regular expression using the split method. The problem is when it displays, it does so with an empty line on the first line and then displays exactly what i need. The problem is that the empty line is [0] and i cannot figure out a way to make the method not display the first empty line. If you run this you will see that it displays a blank line and then displays a few lines of text. I need the blank line gone. Code below


    Java Code:
    public static void stringTrim(){
            String str = "? ? today December 2011 monthweekday Sun 1/15 Mon 1/16 Tue 1/17 Wed 1/18 Thu 1/19 Fri 1/20 Sat 1/21 Sun Mon Tue Wed Thu Fri Sat 27 28 29 30 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 1 2 3 4 5 6 7 9a Any Time Test2 Testing9679 Call back 9a Any Time ALLISON MORI Call back 9a Any Time DEWEY DUCK notes notez notes 9:30a Any Time Refinance Harp TASK TEST 10a Any Time J April TASK TEST 10:30a Any Time Bugs Bunny Call back 9a Any Time TESTTESTTEST TESTTESTTESTTEST Call back 9a Any Time john cocktsten Call back 9a Any Time NICHOLAUS JOHNSON TASK TEST 9a Any Time GUADALUPE TREVINO Call back 9:30a Any Time TESTINGA EVERBANK TASK TEST 9:30a Any Time KRYSTAL LEAKE TASK TEST 9a Any Time Robert sinenomme Call back 9a Any Time ARVIND AGARAWAL TASK TEST ";
            String result[] = str.replaceAll("(\\? \\? today (January|February|March|April|May|June|July|August|September|October|November|December) \\d{4} monthweekday Sun \\d+/\\d+ Mon \\d+/\\d+ Tue \\d+/\\d+ Wed \\d+/\\d+ Thu \\d+/\\d+ Fri \\d+/\\d+ Sat \\d+/\\d+ )|(Sun Mon Tue Wed Thu Fri Sat( \\d{1,2})+)(?![a|p])","").split("(?=(?<![0-9\\:])(\\d+(\\:\\d+)?(a|p)\\s)(?![0-9\\:]))");
            List<String> wordlist = Arrays.asList(result);
            for (Iterator<String> it = wordlist.iterator(); it.hasNext();) {
                  String s = it.next();
                  String strim = s.trim();        
                  System.out.println(strim);              
            }
      }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: Regular Expression Help Needed

    Can you please try:
    Java Code:
    		String str = "? ? today December 2011 monthweekday Sun 1/15 Mon 1/16 Tue 1/17 Wed 1/18 Thu 1/19 Fri 1/20 Sat 1/21 Sun Mon Tue Wed Thu Fri Sat 27 28 29 30 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 1 2 3 4 5 6 7 9a Any Time Test2 Testing9679 Call back 9a Any Time ALLISON MORI Call back 9a Any Time DEWEY DUCK notes notez notes 9:30a Any Time Refinance Harp TASK TEST 10a Any Time J April TASK TEST 10:30a Any Time Bugs Bunny Call back 9a Any Time TESTTESTTEST TESTTESTTESTTEST Call back 9a Any Time john cocktsten Call back 9a Any Time NICHOLAUS JOHNSON TASK TEST 9a Any Time GUADALUPE TREVINO Call back 9:30a Any Time TESTINGA EVERBANK TASK TEST 9:30a Any Time KRYSTAL LEAKE TASK TEST 9a Any Time Robert sinenomme Call back 9a Any Time ARVIND AGARAWAL TASK TEST ";
    		String result[] = str.replaceAll("(\\? \\? today (January|February|March|April|May|June|July|August|September|October|November|December) \\d{4} monthweekday Sun \\d+/\\d+ Mon \\d+/\\d+ Tue \\d+/\\d+ Wed \\d+/\\d+ Thu \\d+/\\d+ Fri \\d+/\\d+ Sat \\d+/\\d+ )|(Sun Mon Tue Wed Thu Fri Sat( \\d{1,2})+)(?![a|p])","").substring(1).split(" (?=(?<![0-9\\:])(\\d+(\\:\\d+)?(a|p)\\s)(?![0-9\\:]))");
    		for (String string : result) {
    			System.out.println(string.trim());
    		}
    is there no way to parse this clean? that looks a bit ugly for me :( (maybe with Matcher and Pattern? ...)
    Last edited by eRaaaa; 01-18-2012 at 11:08 PM.

  3. #3
    radgator is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default Re: Regular Expression Help Needed

    That worked perfectly. Thank you very much. As far as the regex goes, it started out much smaller having less things stripped from it. As requirements changed it was sort of just added to. To answer your question, yes it could be cleaned up a bit...

    Thanks again

Similar Threads

  1. Java regular expression optimization - help needed
    By dpkcv in forum Advanced Java
    Replies: 0
    Last Post: 09-30-2011, 08:42 AM
  2. Regular Expression Help
    By niketanand in forum Advanced Java
    Replies: 1
    Last Post: 06-24-2011, 04:56 PM
  3. Regular Expression Help needed PLease!!
    By hariz2410 in forum New To Java
    Replies: 4
    Last Post: 12-14-2010, 09:46 PM
  4. regular expression
    By prof.deedee in forum JDBC
    Replies: 3
    Last Post: 02-19-2010, 11:15 AM
  5. regular expression
    By ras_pari in forum Advanced Java
    Replies: 27
    Last Post: 10-07-2009, 12:25 PM

Posting Permissions

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