Jump to content

java characters

oliverjrose99

hi

 

i have a java method which checks if a string contains specific chars but i need it to check ones like {, }, ", etc but when i put it in the code if shows an error. how do i fix this.

Pattern pattern = Pattern.compile("{}";");

thanks

Link to comment
Share on other sites

Link to post
Share on other sites

Change it to

Pattern pattern = Pattern.compile('{}";');

EDIT: full explanation

You start a string with the first ", then you stop it with ", then follows a semicolon which is unexpected because the closing bracket of your method call hasn't been entered yet. The next this is another ", which starts a new string and contains the closing bracket and the semicolon you actually want. Changing the outer most " to a single ' solves the problem because the string won't end end when it encounters a ".

 

I hope this makes even a bit of sense :)

Link to comment
Share on other sites

Link to post
Share on other sites

That line looks completely incorrect... You should look at where you are starting strings. Also, you can try looking for them through their integer value. Look at ASCII chart for reference.

Link to comment
Share on other sites

Link to post
Share on other sites

In regex the characters { and } are special. The Pattern class may be smart enough to ignore it since they are empty but you should still escape them imo.

Pattern pattern = Pattern.compile('\{\}";');

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×