4

I am looking for some tips on how I can take a string like:

KIGABCCA TQABCCAXT
GABCCASZYU GZTTABCCA    MHNBABCCA   CLZGABCA    ABCCALZH
ABCCADQRNS VIZABCCA GABCCAG
UEKABCCA KBTOABCCA  GABCCAMFFJ  HABCCAISOJ  OFJJABCCA   HPABCCA
WBXRABCCA
ABCCAKH
VABCCAJX WBDOABCCA ABCCAWM GCABCA   QHRABCCA
ABCCAMDDD   WPABCCAD    OGABCCA
TVABCCA JGLABCA
IUABCCA

and to return any entire string with only one C in it.

PLEASE NOTE: I AM NOT LOOKING FOR A SOLUTION!

Just some pointers or a description of the sort of constructs I should be looking at.

I have been labouring over it for ages, and have come close to hurting someone because of this. It is a homework question and I'm not looking to cheat, just some guidance.

I have read extensively about Reg Ex and I understand them.

I'm not looking for a beginners guide.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Alex
  • 4,844
  • 7
  • 44
  • 58

3 Answers3

3

You want to first put a word boundary at the start and end. Then match any character that isn't C or a word boundary 0 or more times, then a C, then again, any character that isn't a C or word boundary 0 or more times. So it'll match a C on it's own, or a C with any non-C characters either (or both) side of it.

The no-C or word boundary you could do in two ways... say "any character that isn't a C or word boundary" or you could say "I want A, B or anything from D-Z". Up to you.

Nathan MacInnes
  • 11,033
  • 4
  • 35
  • 50
  • 1
    OP states that he doesn't want the solution, but some guidance and a description of the constructs he needs. You've solved the problem for him. – Justin Morgan - On strike Jan 20 '11 at 15:20
  • I am using `Pattern p = Pattern.compile("\b[^C\b]*C[^C\b]*\b"); Matcher m = p.matcher(data);` ... But it appears to be empty of groups. – Alex Jan 20 '11 at 15:22
  • Apologies for posting "solution". I assumed when I posted that it was incomplete and there was something I haven't thought of, but actually, it did seem pretty complete. I edited hoping that @AlexW didn't see the original, but he got in there with a reply while I was editing. – Nathan MacInnes Jan 20 '11 at 15:26
  • @AlexW, try second option for "no C or word boundary", and take out all the \bs because they won't be needed anymore. – Nathan MacInnes Jan 20 '11 at 15:32
  • 1
    Inside a character class, `\b` matches a backspace character. Not that anyone would ever want to match a backspace, but it can't perform its usual role because a word boundary is a zero-width assertion, and a character class always matches exactly one character. I think what you're trying for is `[^C\W]*` -- not a `C` and not a non-word character. – Alan Moore Jan 20 '11 at 15:34
  • @Alan Moore - I am now using `\\b[^C\\w]*C[^C\\w]*\\b` .. surely I want any word character except C? This is not working : ( – Alex Jan 20 '11 at 16:05
  • That won't work. It doesn't allow C or any word character (of which C is one, making the C redundant) I'd make it what Alan suggested. – Nathan MacInnes Jan 20 '11 at 16:08
  • I'm now using `\\b[ABD-Z]*C[ABD-Z]*\\b` .. still nothing. – Alex Jan 20 '11 at 16:12
3

Search for a pattern that has the following elements, in order:

  1. The beginning of the string or any whitespace.
  2. Zero or more non-whitespace non-C characters.
  3. A "C"
  4. Zero or more non-whitespace non-C characters.
  5. The end of the string or any whitespace.
Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
  • Y'know, I've ried this... and it works in principle, but in practice I've had problems... Thanks for the tips – Alex Jan 20 '11 at 15:57
  • @AlexW, the actual regex (as a Java string literal) would be `"(\\A|\\s)[^C\\s]*C[^C\\s]*(\\s|\\z)"` -- is that what you tried? And are you using the `find()` method to apply the regex? – Alan Moore Jan 20 '11 at 16:34
  • @Alan - I've got it sorted now, based on the tips from SO. Am now using find() too. Appreciate it. Please leave an answer and I can mark as correct? – Alex Jan 20 '11 at 16:37
0

you can create a count function. then pass each string to it. just an example

String string = "KIGABCCA"
public static boolean countChar(String string, char ch){
     int count =0;
     for(int i = 0; i<string.length();i++){
        if(string.charAt(i) == ch ){
          count++;
        }
     }
     if ( count == 1){ 
       return true;
     }else {
       return false;
     }
}
ghostdog74
  • 327,991
  • 56
  • 259
  • 343