0

With java regex i want to find the word "C++" and it should not be positive with only "C".

The below code should explain the rest, see here

import java.util.*; import java.util.regex.*; public class MyClass { public static void main(String args[]) { String test = "Framework, c++ and Visual Studio IDEs."; Pattern p = Pattern.compile("(?i).*\\bc\\+\\+\\b.*"); Matcher m = p.matcher(test); if(m.find()) { System.out.println("Pattern1 True"); } p = Pattern.compile("(?i).*\\Bc.+.+\\B.*"); m = p.matcher(test); if(m.find()) { System.out.print("Pattern2 True"); } p = Pattern.compile("(?i).*\\bc+$\\b.*"); m = p.matcher(test); if(m.find()) { System.out.println("Pattern3 is True but how to return false"); } p = Pattern.compile("(?i).*\\Bc\\B.*"); m = p.matcher(test); if(m.find()) { System.out.println("Pattern4 is True"); } if(test.toLowerCase() .contains("c++")) { System.out.print("Contains c++ True"); } if(test.toLowerCase().contains("C")) { System.out.print("Contains C True"); } } } 
4
  • 1
    Use a non-word boundary, Pattern p = Pattern.compile("(?i).*\\bc\\+\\+\\B.*"); Commented Nov 16, 2022 at 20:50
  • @WiktorStribiżew nice one with C++, will this work with only "C" Commented Nov 16, 2022 at 20:56
  • If you want to match C not followed with ++, use \bC\b(?!\+\+) Commented Nov 16, 2022 at 21:07
  • @WiktorStribiżew 👍 (?i).*\\bC\\b(?!\\+\\+).* Commented Nov 16, 2022 at 21:16

3 Answers 3

1

You can use

Pattern p = Pattern.compile("(?i).*\\bc\\+\\+\\B.*"); // C++ Pattern p = Pattern.compile("(?i).*\\bC\\b(?!\\+{2}).*"); // C only 

Here,

  • \b - a word boundary
  • c\+\+ - a c++ string
  • \B - a non-word boundary, on the right, there must be end of string or a non-word char
  • (?!\+{2}) - a negative lookahead that fails the match if there is a ++ string immediately to the right of the current location.
Sign up to request clarification or add additional context in comments.

2 Comments

for omitting all appending characters with C - "(?i).*\\bC\\b(?!\\+|#|\\$|\*|\\^|%|&).*" Phew is there is anyway to do a asterisk with those omitting characters, can we propose the regex community to do so?
@PradyutBhattacharya You need to make sure what your "word" boundary is like. If you can describe it using a finite amount of special chars, then the above pattern is a solution. If you can describe it in any other way, please do and we'll be able to adjust the pattern.
0

This seems to work at detecting either "C++" or "c++" anywhere in an input string.

String regex = ".*[cC]\\+\\+.*"; java.util.regex.Pattern.matches(regex, "This is NOT c++ !"); 

Results in true, but

java.util.regex.Pattern.matches(regex, "This is NOT c!"); 

results in false.

1 Comment

how to find only c and false for c++
0

tough time omitting characters for only C

p = Pattern.compile("(?i).*\\bC\\b(?!\\+|#|\\$|\\*|\\^|%|&|\\.).*"); m = p.matcher(test); if(m.find()) { System.out.println("C is true"); } p = Pattern.compile("(?i).*\\bc\\+\\+\\B.*"); m = p.matcher(test); if(m.find()) { System.out.println("C++ is true"); } p = Pattern.compile("(?i).*\\bc\\#\\B.*"); m = p.matcher(test); if(m.find()) { System.out.println("C# is true"); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.