Results 1 to 9 of 9
Thread: Pattern expression for strings
- 04-06-2010, 03:21 AM #1
Member
- Join Date
- Apr 2010
- Location
- At my desk
- Posts
- 9
- Rep Power
- 0
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??
Thanks in advance...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; }
-
I don't know.
But this worries me:
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?Java Code:for (j = 0; j < combo1.size(); j++) { boolean matches = p.matcher(combo1.get(j)).matches(); if ((matches == false)) { combo1.remove(j); } }
- 04-06-2010, 04:08 AM #3
Member
- Join Date
- Apr 2010
- Location
- At my desk
- Posts
- 9
- Rep Power
- 0
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;
- 04-06-2010, 04:18 AM #4
Member
- Join Date
- Apr 2010
- Location
- At my desk
- Posts
- 9
- Rep Power
- 0
Thanks Furable
for being concerned about the loop thing (:
Finally around it with
...and it works!!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; } }
-
- 04-06-2010, 05:08 AM #6
Member
- Join Date
- Apr 2010
- Location
- At my desk
- Posts
- 9
- Rep Power
- 0
-
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!
- 04-06-2010, 05:32 AM #8
Member
- Join Date
- Apr 2010
- Location
- At my desk
- Posts
- 9
- Rep Power
- 0
- 04-06-2010, 05:35 AM #9
Member
- Join Date
- Apr 2010
- Location
- At my desk
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Quadratic Expression
By c3jcarmy in forum New To JavaReplies: 7Last Post: 11-28-2009, 06:16 PM -
regular expression
By ras_pari in forum Advanced JavaReplies: 27Last Post: 10-07-2009, 12:25 PM -
is it valid expression in jsp adding two strings with + sign
By raghu2114 in forum Advanced JavaReplies: 7Last Post: 04-09-2009, 03:24 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks