Results 1 to 9 of 9
  1. #1
    chico9 is offline Member
    Join Date
    Apr 2010
    Location
    At my desk
    Posts
    9
    Rep Power
    0

    Angry Pattern expression for strings

    The following code, no matter what I do, doesn't seem to be matching the strings I want.

    Say, after the first bit where it compiles a pattern, I get the pattern:

    0[01][01]1[01][01][01][01]0

    and then I print the list (combo1), it gives:

    Combo1

    000001110 <--- this shouldn't be here as it should've been removed, somehow it matches the pattern!

    000111000

    001110000

    011100000

    Is there something wrong with my pattern expression??

    Java Code:
    	public static List<String> combos2 (List<String> combo1, Line line)
    	{
    		int i, j;
    		StringBuffer temp = new StringBuffer();
    		String y;
    		
    
    		for (i = 0; i < line.size(); i++)
    		{
    			if (line.getCellAt(i).getValue() == Value.BLACK)
    			{
    				temp.append('1');
    			}
    			else if (line.getCellAt(i).getValue() == Value.WHITE)
    			{
    				temp.append('0');
    			}
    			else
    			{
    				temp.append('2');
    			}
    		}
    
    		y = temp.toString();
    		y = y.replaceAll("2", "[01]");
    		
    		Pattern p = Pattern.compile(y);
    		
    		System.out.println("here's x:   " + p.toString() + "\n");
    		
    		for (j = 0; j < combo1.size(); j++)
    		{
    			boolean matches = p.matcher(combo1.get(j)).matches();
    			if ((matches == false))
    			{
    				combo1.remove(j);
    			}
    		}
    		return combo1;
    	}
    Thanks in advance...

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

    Default

    Quote Originally Posted by chico9 View Post
    Is there something wrong with my pattern expression??
    I don't know.

    But this worries me:
    Java Code:
    		for (j = 0; j < combo1.size(); j++)
    		{
    			boolean matches = p.matcher(combo1.get(j)).matches();
    			if ((matches == false))
    			{
    				combo1.remove(j);
    			}
    		}
    Are you sure that you shouldn't be using an iterator here if you are going to be removing items from the list while looping?

  3. #3
    chico9 is offline Member
    Join Date
    Apr 2010
    Location
    At my desk
    Posts
    9
    Rep Power
    0

    Default Hmm, you're right, but

    I've tried the following (adding the matches to another list) but now I've got more strings in the resulting list that don't even match!

    Java Code:
    List<String> tempL = new ArrayList<String>();
    		
    		for (j = 0; j < combo1.size(); j++)
    		{
    			boolean matches = p.matcher(combo1.get(j)).matches();
    			if ((matches == true))
    			{
    				tempL.add(combo1.get(j).toString());
    			}
    		}
    		combo1 = tempL;

  4. #4
    chico9 is offline Member
    Join Date
    Apr 2010
    Location
    At my desk
    Posts
    9
    Rep Power
    0

    Talking Thanks Furable

    for being concerned about the loop thing (:

    Finally around it with

    Java Code:
    		for (j = 0; j < combo1.size(); j++)
    		{
    			boolean matches = p.matcher(combo1.get(j)).matches();
    			if ((matches == false))
    			{
    				combo1.remove(j);
    				j = -1;
    			}
    		}
    ...and it works!!

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

    Default

    Quote Originally Posted by chico9 View Post
    for being concerned about the loop thing (:

    Finally around it with
    ...and it works!!
    And it's a bad kludge that will bite you in the tail; just don't do it. Why not use an iterator as I suggested earlier?

  6. #6
    chico9 is offline Member
    Join Date
    Apr 2010
    Location
    At my desk
    Posts
    9
    Rep Power
    0

    Default Thanks

    Quote Originally Posted by Fubarable View Post
    And it's a bad kludge that will bite you in the tail; just don't do it. Why not use an iterator as I suggested earlier?
    Will do next time round; deadline creeping up (:

    Kudos.

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

    Default

    Quote Originally Posted by chico9 View Post
    Will do next time round; deadline creeping up (:

    Kudos.
    Gol this leaves such a bad taste in my mouth because it is a very very bad idea to modify the index from within the loop. The iterator solution is so easy that it's a shame not to use it.

    OK, I'm dropping this but I had to say something again. Also, if this were for a class, I'd ding ya. If for a job, I'd fire ya. Best of luck regardless!

  8. #8
    chico9 is offline Member
    Join Date
    Apr 2010
    Location
    At my desk
    Posts
    9
    Rep Power
    0

    Default ...

    Quote Originally Posted by Fubarable View Post
    Also, if this were for a class, I'd ding ya. If for a job, I'd fire ya. Best of luck regardless!
    :eek: i should study more i guess, and thanks.

  9. #9
    chico9 is offline Member
    Join Date
    Apr 2010
    Location
    At my desk
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    The iterator solution is so easy that it's a shame not to use it.
    Now you got me curious! How would I use an iterator to implement this? Thanks

Similar Threads

  1. Quadratic Expression
    By c3jcarmy in forum New To Java
    Replies: 7
    Last Post: 11-28-2009, 07:16 PM
  2. regular expression
    By ras_pari in forum Advanced Java
    Replies: 27
    Last Post: 10-07-2009, 01:25 PM
  3. Replies: 7
    Last Post: 04-09-2009, 04:24 AM

Tags for this Thread

Posting Permissions

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