I have a phone number like (123) 456-7890. I am using the replaceAll method to remove () and - and spaces from the string. I tried following
String phNo= "(123) 456-7890".replaceAll("[()-\\s]").trim();
but it's not working. Any solution?
This should work:
String phNo = "(123) 456-7890".replaceAll("[()\\s-]+", "");
In your regex:
\s should be \\s
Hyphen should be first or last in character class to avoid escaping or use it as \\-
Use quantifier + as in [()\\s-]+ to increase efficiency by minimizing # of replacements
If you want the phone number then use:
String phNo = "(123) 456-7890".replaceAll("\\D+", "");
This regex will mark all characters that are not digits, and replace them with an empty string.
The regex: \D+
Match a single character that is not a digit. \D
Between one and unlimited times, as many times as possible. +
String newStr = phoneNumber.replaceAll("[^0-9]", "");
System.out.println(newStr);
Removes All Non-Digit Characters.
Java Regex - Tutorial
IN 2022 use this! all other answers too old!!!!!!!!!!!
result = "(123) 456-7890".replace(/[^+\d]+/g, "");
The - character with brackets [] indicates a character range, e.g. [a-z]. However, the character range doesn't work here where you want a literal - to be used. Escape it.
String phNo = "(123) 456-7890".replaceAll("[()\\-\\s]", "").trim());
There are two main reasons this does not work as expected.
Inside of a character class the hyphen has special meaning. You can place a hyphen as the first or last character of the class. In some regular expression implementations, you can also place directly after a range. If you place the hyphen anywhere else you need to escape it in order to add it to your class.
String phNo = "(123) 456-7890".replaceAll("[()\\-\\s]").trim();
^^
You are not supplying a replacement value which neither answer has pointed out to you.
String phNo = "(123) 456-7890".replaceAll("[()\\-\\s]", "").trim();
^^
And finally, you can remove .trim() here as well.
String phNo = "(123) 456-7890".replaceAll("[()\\-\\s]", "");
If you are using Kotlin than
mobileNo.replace(Regex("[()\\-\\s]"), "")
Related
I need to replace ALL characters that don't follow a pattern with "".
I have strings like:
MCC-QX-1081
TEF-CO-QX-4949
SPARE-QX-4500
So far the closest I am using the following regex.
String regex = "[^QX,-,\\d]";
Using the replaceAll String method I get QX1081 and the expected result is QX-1081
You're using a character class which matches single characters, not patterns.
You want something like
String resultString = subjectString.replaceAll("^.*?(QX-\\d+)?$", "$1");
which works as long as nothing follows the QX-digits part in your strings.
Put the dash at the end of the regex: [^QX,\d-]
Next you just have to substring to filter out the first dash.
Don't know exactly what you expect for all strings but if you want to match a dash in a character class then it must be set as last character.
You are using a character class where you have to either escape the hyphen or put it at the start or at the end like [^QX,\d-] or else you are matching a range from a comma to a comma. But changing that will give you -QX-1081 which is not the desired result.
You could match your pattern and then replace with the first capturing group $1:
^(?:[A-Z]+-)+(QX-\d+)$
In Java you have to double escape matching a digit \\d
That will match:
^ Start of the string
(?:[A-Z]+-)+ Repeat 1+ times one or more uppercase charactacters followed by a hyphen
(QX-\d+) Capture in a group QX- followed by 1+ digits
$ End of the string
For example:
String result = "MCC-QX-1081".replaceAll("^(?:[A-Z]+-)+(QX-\\d+)$", "$1");
System.out.println(result); // QX-1081
See the Regex demo | Java demo
Note that if you are doing just 1 replacement, you could also use replaceFirst
I need to replace all the 's around numbers, to nothing.. for example:
'1' to 1
'100' to 100
which is the optimal way to do this? is there a regex to do this so I can use it in the replace() function of the String class?
You can use replaceAll method with regex support:
str = str.replaceAll("'(\\d+)'", "$1");
(\\d+) will match and group digits surrounded by single quotes on either side and then we use $1 in replacement which is the back-reference to captured value in regex.
If it's in a String and you want the integer why don't you just parse it.
int a = Integer.parseInt("100");
Say I have a few string like Foo3,5bar, Foo14,5bar and Foo23,42bar
I want to remove the second number, following the comma, as well as the comma, using Java Regex.
So far, I've tried String.replaceAll("(?<=Foo\d{1,2}),\d{1,2}", ""), using (?<=Foo\d{1,2}),\d{1,2} as my regex, but it's not working.
Use String#replaceAll that has regex support:
String str = "Foo3,4HelloFoo5,3World";
str = str.replaceAll("(\\d),\\d+", "$1"); // Foo3HelloFoo5World
OR else if you want to restrict matching to max 2 digits after comma then use:
str = str.replaceAll("(\\d),\\d{1,2}", "$1"); // Foo3HelloFoo5World
Live Demo: http://ideone.com/5P1guJ
str = str.replaceFirst(",\\d+$")
what about Integer.valueOf(str.substring(string.lastIndexOf(",")+1));
is regex a necessity ?
Your regex is almost correct. You forget to escape the \ in the regex. The correct one is:
(?<=Foo\\d{1,2}),\\d{1,2}
Note the \\ instead of \.
See https://ideone.com/W7IuT1 for a demo on your strings.
I need to remove punctuation following a word. For example, word?! should be changed to word and string: should be changed to string.
Edit: The algorithm should only remove punctuation at the end of the String. Any punctuation within the String should stay. For instance, doesn't; should become doesn't.
Use the method replaceAll(...) which accept a regular expression.
String s = "don't. do' that! ";
s = s.replaceAll("(\\w+)\\p{Punct}(\\s|$)", "$1$2");
System.out.println(s);
You could use a regex to modify the string.
String resultString = subjectString.replaceAll("([a-z]+)[?:!.,;]*", "$1");
There are no "words" that I know of where ' is at the end and it is used as a punctuation. So this regex will work for you.
What regex pattern would need I to pass to java.lang.String.split() to split a String into an Array of substrings using all whitespace characters (' ', '\t', '\n', etc.) as delimiters?
Something in the lines of
myString.split("\\s+");
This groups all white spaces as a delimiter.
So if I have the string:
"Hello[space character][tab character]World"
This should yield the strings "Hello" and "World" and omit the empty space between the [space] and the [tab].
As VonC pointed out, the backslash should be escaped, because Java would first try to escape the string to a special character, and send that to be parsed. What you want, is the literal "\s", which means, you need to pass "\\s". It can get a bit confusing.
The \\s is equivalent to [ \\t\\n\\x0B\\f\\r].
In most regex dialects there are a set of convenient character summaries you can use for this kind of thing - these are good ones to remember:
\w - Matches any word character.
\W - Matches any nonword character.
\s - Matches any white-space character.
\S - Matches anything but white-space characters.
\d - Matches any digit.
\D - Matches anything except digits.
A search for "Regex Cheatsheets" should reward you with a whole lot of useful summaries.
To get this working in Javascript, I had to do the following:
myString.split(/\s+/g)
"\\s+" should do the trick
Also you may have a UniCode non-breaking space xA0...
String[] elements = s.split("[\\s\\xA0]+"); //include uniCode non-breaking
String string = "Ram is going to school";
String[] arrayOfString = string.split("\\s+");
Apache Commons Lang has a method to split a string with whitespace characters as delimiters:
StringUtils.split("abc def")
http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#split(java.lang.String)
This might be easier to use than a regex pattern.
All you need is to split using the one of the special character of Java Ragex Engine,
and that is- WhiteSpace Character
\d Represents a digit: [0-9]
\D Represents a non-digit: [^0-9]
\s Represents a whitespace character including [ \t\n\x0B\f\r]
\S Represents a non-whitespace character as [^\s]
\v Represents a vertical whitespace character as [\n\x0B\f\r\x85\u2028\u2029]
\V Represents a non-vertical whitespace character as [^\v]
\w Represents a word character as [a-zA-Z_0-9]
\W Represents a non-word character as [^\w]
Here, the key point to remember is that the small leter character \s represents all types of white spaces including a single space [ ] , tab characters [ ] or anything similar.
So, if you'll try will something like this-
String theString = "Java<a space><a tab>Programming"
String []allParts = theString.split("\\s+");
You will get the desired output.
Some Very Useful Links:
Split() method Best Examples
Regexr
split-Java 11
RegularExpInfo
PatternClass
Hope, this might help you the best!!!
To split a string with any Unicode whitespace, you need to use
s.split("(?U)\\s+")
^^^^
The (?U) inline embedded flag option is the equivalent of Pattern.UNICODE_CHARACTER_CLASS that enables \s shorthand character class to match any characters from the whitespace Unicode category.
If you want to split with whitespace and keep the whitespaces in the resulting array, use
s.split("(?U)(?<=\\s)(?=\\S)|(?<=\\S)(?=\\s)")
See the regex demo. See Java demo:
String s = "Hello\t World\u00A0»";
System.out.println(Arrays.toString(s.split("(?U)\\s+"))); // => [Hello, World, »]
System.out.println(Arrays.toString(s.split("(?U)(?<=\\s)(?=\\S)|(?<=\\S)(?=\\s)")));
// => [Hello, , World, , »]
Since it is a regular expression, and i'm assuming u would also not want non-alphanumeric chars like commas, dots, etc that could be surrounded by blanks (e.g. "one , two" should give [one][two]), it should be:
myString.split(/[\s\W]+/)
you can split a string by line break by using the following statement :
String textStr[] = yourString.split("\\r?\\n");
you can split a string by Whitespace by using the following statement :
String textStr[] = yourString.split("\\s+");
String str = "Hello World";
String res[] = str.split("\\s+");
Study this code.. good luck
import java.util.*;
class Demo{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.print("Input String : ");
String s1 = input.nextLine();
String[] tokens = s1.split("[\\s\\xA0]+");
System.out.println(tokens.length);
for(String s : tokens){
System.out.println(s);
}
}
}