JhonF
JhonF

Reputation: 199

Want to check the curly brace existence. But return false

I tried to check the curly braces existence of a String. But it returns false. Can anyone give me a solution.

String code = "class Demo{public static void main(String[] args) {System.out.println(\"ABC\");}}";
Pattern p = Pattern.compile("^[{]");
Matcher m = p.matcher(code);
System.out.println(m.matches());

Thanks in Advance

Upvotes: 1

Views: 188

Answers (4)

Bernhard Barker
Bernhard Barker

Reputation: 55619

^ means the start of the string. It should be removed, since you want to find it anywhere in the string.

Oh and don't use matches, use find instead. matches checks whether the whole string matches the pattern, find looks for the pattern in the string.

String code = "class Demo{public static void main(String[] args) {System.out.println(\"ABC\");}}";
Pattern p = Pattern.compile("[{]");
Matcher m = p.matcher(code);
System.out.println(m.find());

Though, contains("{"), as Rohit mentioned, would be simpler.

String code = "class Demo{public static void main(String[] args) {System.out.println(\"ABC\");}}";
System.out.println(code.contains("{"));

If you want to do a replacement, Matcher does have a replaceAll function, but so does String. This adds a new-line before every {: (\\{ is an alternate way of escaping {)

String code = "class Demo{public static void main(String[] args) {System.out.println(\"ABC\");}}";
System.out.println(code.replaceAll("\\{", "\n{"));

Now if I'm correct in where you're going with this, you can't do code indentation with regex. It's incremental / recursive, which doesn't work (well) in regex. You'll need to iterate through the string manually to do this.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533720

Needs comments

// the first character must be a {
Pattern p = Pattern.compile("^[{]");
Matcher m = p.matcher(code);
// the entire strings must match so you only accept "{" and nothing else.
System.out.println(m.matches());

I suspect you didn't want ^ and you wanted find() instead of matches() Find will accept the first substring which matches.

Upvotes: 2

joescii
joescii

Reputation: 6533

Either use code.contains("{") as @Rohit Jain suggests, or use this as your regex:

Pattern p = Pattern.compile("^*.[{]*.");

Without the wildcards, the pattern will only match exactly one string, namely "{"

Upvotes: 0

StormeHawke
StormeHawke

Reputation: 6207

Your regex doesn't allow for other characters besides what is included. Also, I fixed your regex - ^ matches at the beginning of the String only, so you'd have to have a { at the beginning of your String.

Matcher.matches() is going to try to make the entire string match the regex. Matcher.find() is checking to see if the regex pattern exists anywhere within the String

  String code = "class Demo{public static void main(String[] args) {System.out.println(\"ABC\");}}";
  Pattern p = Pattern.compile("[{]");
  Matcher m = p.matcher(code);
  System.out.println(m.find());

Upvotes: 1

Related Questions