Results 1 to 8 of 8
  1. #1
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Issue with regex expression parameter supplied to String split method.

    I created a small test program below to to test the String split function. Its not working as I would expect it to work.

    I expect the program to print:
    Parts of the myString = #if
    Parts of the myString = ABC
    Parts of the myString= CCC
    Parts of the myString = DDD

    However, the program generates:
    Parts of the myString = #if
    Parts of the myString = ABC
    Parts of the myString =
    Parts of the myString =
    Parts of the myString =
    Parts of the myString = CCC
    Parts of the myString =
    Parts of the myString =
    Parts of the myString =
    Parts of the myString = DDD

    Also, if I replace '#if ABC && CCC || DDD" with #if ABC & CCC | DDD", the program still generates the same results.

    Furthermore, I realize | is a special character, but I get an compile time error if I change it to \|{2}.

    Could someone please enlighten me. Thanks, Stephen


    Java Code:
    public class TestRegex {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
            String regexString = "[()!&{2}|{2} ]" ;
    
            String myString = "#if ABC && CCC || DDD";
    
            String[] myStringArray = myString.split(regexString);
    
            for (String parts : myStringArray)
            {
                System.out.println("Parts of the myString = " + parts);
            }
    
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Issue with regex expression parameter supplied to String split method.

    Your regex splits on every char contained in your square brackets, so it should be no surprise that you're getting the result you see. Why not instead use a String that holds variable amount of whitespace in front and behind, and then in parenthesis has a choice of space, or && or || (the latter escaped)? When I do this, my result is:


    Parts of the myString = #if
    Parts of the myString = ABC
    Parts of the myString = CCC
    Parts of the myString = DDD

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Issue with regex expression parameter supplied to String split method.

    For example...

    Java Code:
    String regex2 = "\\s*( |&{1,2}|\\|{1,2})\\s*";

  4. #4
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Issue with regex expression parameter supplied to String split method.

    Do you mean something like this?: String regexString = "[\\s*( &&\\|\\|)\\s*]"

    For that expression my program returns:
    Parts of the myString = #if
    Parts of the myString = ABC
    Parts of the myString = &&
    Parts of the myString = CCC
    Parts of the myString = ||
    Parts of the myString = DDD

    If I use String regexString = "[\\s*( &{2}\\|\\|)\\s*]", I get the same result as before.

    Also, using a single escape character before | (e.g. \|) shows up as an error.

  5. #5
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Issue with regex expression parameter supplied to String split method.

    I just noticed you posted the regex, so you can disregard post #4.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Issue with regex expression parameter supplied to String split method.

    Your problem is that you're using the square brackets where you shouldn't. Those are for extracting a single character only when you want a complete String.

  7. #7
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Issue with regex expression parameter supplied to String split method.

    Fantastic! That works. Thanks.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Regular Expression issue and setName() method issue
    By geforce in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 04:33 AM
  2. Using the split() method in the string class
    By Jdelacroix in forum New To Java
    Replies: 2
    Last Post: 03-27-2011, 11:39 AM
  3. String split method
    By DavidG24 in forum New To Java
    Replies: 5
    Last Post: 12-14-2010, 03:01 PM
  4. String split method
    By counterfox in forum New To Java
    Replies: 2
    Last Post: 11-13-2010, 12:06 AM
  5. Please help me with Split method> String
    By javanew in forum New To Java
    Replies: 5
    Last Post: 04-02-2010, 07:15 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
  •