0

I need to remove ^A from an incoming string, I'm looking for a regex pattern for it

Don’t want to use \\p{cntrl} , I don’t want to delete the other control characters coming in the string

5
  • You can escape special characters for an exact match of that character. You can use sites such as this one to test your regexes easily. Commented Sep 16, 2019 at 8:28
  • @Someprogrammerdude I have tried all possible combinations it’s not working Commented Sep 16, 2019 at 8:36
  • Any reason why you need to use regex to remove a fixed substring? Basic string manipulation would be more efficient and require no regex knowledge. Commented Sep 16, 2019 at 9:13
  • It’s a parameterized that’s to be removed Commented Sep 16, 2019 at 9:14
  • As long as the parameter itself isn't a regex it doesn't matter. I'll post a regex-less answer for illustration Commented Sep 16, 2019 at 9:23

2 Answers 2

1

You should use escaping for '^A':

public static void main(String[] args) { String value = "remove special char ^A A and B"; System.out.println(value.replaceAll("\\^A", "")); } 

Output:

remove special char A and B

Sign up to request clarification or add additional context in comments.

7 Comments

Try using this string “remove special char ^A A and B”
If I use \\^A it removes my stand alone “A” as well output is like “remove special char and B”
@megharaina: You're using forward slashes there.
Wrote it mistakenly , using the correct escape only
Can u pls try it with the string that gave ?
|
0

I suggest you avoid using regex and instead use basic string manipulation :

String toRemove = "^A"; yourString.replace(toRemove, ""); 

You can try it here.

Be careful not to use String methods that work on regexs, especially since the name of the methods aren't very informative (replace replaces all occurences of a fixed string, replaceAll replaces all occurences that match a regex pattern).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.