Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all characters not in range (Java String)

How do you replace all of the characters in a string that do not fit a criteria. I'm having trouble specifically with the NOT operator.

Specifically, I'm trying to remove all characters that are not a digit, I've tried this so far:

String number = "703-463-9281";
String number2 = number.replaceAll("[0-9]!", ""); // produces: "703-463-9281" (no change)
String number3 = number.replaceAll("[0-9]", "");  // produces: "--" 
String number4 = number.replaceAll("![0-9]", ""); // produces: "703-463-9281" (no change)
String number6 = number.replaceAll("^[0-9]", ""); // produces: "03-463-9281"
like image 726
Chris Dutrow Avatar asked Oct 02 '10 19:10

Chris Dutrow


People also ask

How do you replace all characters except numbers in Java?

As you might guess, you can strip all characters but letters and numbers by making a minor change to the replaceAll regular expression, like this: aString. replaceAll("[^a-zA-Z0-9]",""); All I did there was add the numbers [0-9] to our previous range of characters.

What is replaceAll \\ s in Java?

The replaceAll() method replaces each substring that matches the regex of the string with the specified text.

How do you replace a character in a String in Java without using replace method?

To replace a character in a String, without using the replace() method, try the below logic. Let's say the following is our string. int pos = 7; char rep = 'p'; String res = str. substring(0, pos) + rep + str.


2 Answers

To explain: The ^ at the start of a character class will negate that class But it has to be inside the class for that to work. The same character outside a character class is the anchor for start of string/line instead.

You can try this instead:

"[^0-9]" 
like image 91
tangens Avatar answered Sep 23 '22 23:09

tangens


Here's a quick cheat sheet of character class definition and how it interacts with some regex meta characters.

  • [aeiou] - matches exactly one lowercase vowel
  • [^aeiou] - matches a character that ISN'T a lowercase vowel (negated character class)
  • ^[aeiou] - matches a lowercase vowel anchored at the beginning of the line
  • [^^] - matches a character that isn't a caret/'^'
  • ^[^^] - matches a character that isn't a caret at the beginning of line
  • ^[^.]. - matches anything but a literal period, followed by "any" character, at the beginning of line
  • [a-z] - matches exactly one character within the range of 'a' to 'z' (i.e. all lowercase letters)
  • [az-] - matches either an 'a', a 'z', or a '-' (literal dash)
  • [.*]* - matches a contiguous sequence (possibly empty) of dots and asterisks
  • [aeiou]{3} - matches 3 consecutive lowercase vowels (all not necessarily the same vowel)
  • \[aeiou\] - matches the string "[aeiou]"

References

  • regular-expressions.info/Character class, Anchors, Dot, Repetition

Related questions

  • Regex: why doesn't [01-12] range work as expected?
  • Character class subtraction, converting from Java syntax to RegexBuddy
  • How can I exclude some characters from a class?
like image 45
polygenelubricants Avatar answered Sep 23 '22 23:09

polygenelubricants