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.

6
  • Is this one big string with whitespaces? Commented Jan 20, 2011 at 15:06
  • That's correct.The solution should return the character string with no whitespace. Commented Jan 20, 2011 at 15:08
  • Did your teacher(?) ask you to perform this with RegEx exclusively? If so, what platform? Commented Jan 20, 2011 at 15:09
  • 1
    @maaartinus he said this is homework. Commented Jan 20, 2011 at 15:11
  • 1
    @maaa, if you read the question he states that it is indeed homework. Commented Jan 20, 2011 at 15:12

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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.
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.
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.
@AlexW, try second option for "no C or word boundary", and take out all the \bs because they won't be needed anymore.
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.
|
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.

3 Comments

Y'know, I've ried this... and it works in principle, but in practice I've had problems... Thanks for the tips
@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 - 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?
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;
     }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.