RegEx to match strings that have only one C

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

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 return the whole whole line with one C in it.

PLEASE NOTE: I AM NOT LOOKING FOR A SOLUTION!

Just some pointers or descriptions of the constructions I should look at.

I worked on this for many years, and because of this I was very hurt. This is a matter of homework, and I do not want to cheat, just be guided.

I read a lot about Reg Ex and I understand them.

I am not looking for a guide for beginners.

+4
source share
3 answers

You want to first put the word boundary at the beginning and at the end. Then match any character that is not C or a word boundary 0 or more times, then C, and then another character that is not a C boundary or word 0 or more times. That way, it will match C on it, or C with any characters other than C, either (or both).

You could do a non-C or word boundary in two ways ... say "any character that is not the boundary of C or a word", or you could say "I want A, B or something from DZ." To you.

+3
source

Find the template that has the following elements in order:

  • The beginning of a line or any space.
  • Zero or more non-whitespace other than C.
  • A "C"
  • Zero or more non-whitespace other than C.
  • End of line or any space.
+3
source

you can create a count function. then pass each line 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; } } 
0
source

Source: https://habr.com/ru/post/1336325/


All Articles