Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

Need help with regular experssion in Java and escape characters

I want to remove the string "{x=0}" from text. Not sure how to represent {

String text;
String regex = "/{"; //this gives compile error: illegal escape character
text = text.replaceAll(regex, "");

if I use the other \{ it will run but gives me run time error about illegal escape sequence ..

aslo how do I use this method to replace a sequence of spaces and tabs from text? what would be the regex?
0
polkadot
Asked:
polkadot
  • 4
  • 2
  • 2
  • +1
3 Solutions
 
CEHJCommented:
String regex = "\\{x=0\\}";
0
 
polkadotAuthor Commented:
and about spaces? see question
0
 
CEHJCommented:
>>what would be the regex?

Missed that sorry - well it would depend on the exact sequence. If it's any range of contiguous whitespace, try

s = s.replaceAll("\\s+", "");
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sudhakar_koundinyaCommented:
\\s
0
 
yellowjetskiCommented:
\s Matches any white space character including space, tab, form-feed, and so on.
0
 
CEHJCommented:
polkadot - i'm confused by your treatment of this question. I answered both the main question here and the 'subquestion' yet

a. the comment marked as the accepted answer relates merely to syntax in a different language.

and

b. The other comment marked as 'assisted' repeats info i'd already given.

 Why?
0
 
sudhakar_koundinyaCommented:
CEHJ ,

The second comment was given in split seconds of time. And I am late.
If you think, you deserve the points. no problem. I am ready to give the points.
And finally it's  polkadot's decision. If he/she is willing to reopen and reassign the points

Regards
Sudhakar

0
 
CEHJCommented:
>>The second comment was given in split seconds of time.

Don't worry - i'm not blaming you sudhakar
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now