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"); } } }
Pattern p = Pattern.compile("(?i).*\\bc\\+\\+\\B.*");Cnot followed with++, use\bC\b(?!\+\+)