AGupta
AGupta

Reputation: 5734

How to use \\p{Punct} in java to check for beginning of text line with: {"

Given a String that begin's with the symbols: {" and ends with: "}. There are other punctuation's present in between the line aswell, like: , ' or "" etc. How to use java regex utility to know whether the given String starts with: {". I am trying to return the Boolean value by using:

Pattern.matches(begin, string) 

where

begin = "[\\p{Punct}&&[{]]" 

and

string = {"name":"Aman"},{"surname":"Gupta"}. 

(Please suggest regex option than JSON) I want to do it by using regex only. Please suggest a way how to achieve this.

Upvotes: 2

Views: 3408

Answers (2)

svz
svz

Reputation: 4588

You should try smth like this:

 Pattern p = Pattern.compile("\{.*?\}");
 Matcher m = p.matcher(/*your string here*/);
 while (m.find()){
     String substringInBraces = m.group();
     /*do smth with your substring*/
 }

This will give you a substring of anything that might be between two nearest curly braces.

You might be interested in reading this and this

Upvotes: 3

gpvos
gpvos

Reputation: 2802

Pattern.compile("^{").matcher(string).find()

I don't know why you insist on using \\p{Punct}, it's totally unnecessary here.

Note that Pattern.matches() wants to match the entire string, so it is not useful when you only want to match something at the start of a string.

Upvotes: 2

Related Questions