Java, replace string numbers with blankstring and remove everything after the numbers - java

I have strings like:
Alian 12WE
and
ANI1451
Is there any way to replace all the numbers (and everything after the numbers) with an empty string in JAVA?
I want the output to look like this:
Alian
ANI

With a regex, it's pretty simple:
public class Test {
public static String replaceAll(String string) {
return string.replaceAll("\\d+.*", "");
}
public static void main(String[] args) {
System.out.println(replaceAll("Alian 12WE"));
System.out.println(replaceAll("ANI1451"));
}
}

You could use a regex to remove everyting after a digit is found - something like:
String s = "Alian 12WE";
s = s.replaceAll("\\d+.*", "");
\\d+ finds one or more consecutive digits
.* matches any characters after the digits

Use Regex
"Alian 12WE".split("\\d")[0] // Splits the string at numbers, get the first part.
Or replace "\\d.+$" with ""

Related

replace last dot in a string with a dollar sign

I would like to replace the last dot in the following string with a dollar sign, how can I do that?
de.java_chess.javaChess.game.GameImpl.GameStatus
I would like to have de.java_chess.javaChess.game.GameImpl$GameStatus instead.
I am using the following line of code to do so:
invokedMeth = invokedMeth.replaceAll("(.*)\\.(\\d+)$","$1$$2");
However, this doesn't work and I end up with the same original string that I had as an input. How can I fix this?
For this requirement, I would use a non-regex solution that can be easier to understand as well as more efficient.
StringBuilder invokedMethSb = new StringBuilder(invokedMeth);
invokedMethSb.setCharAt(invokedMethSb.lastIndexOf("."), '$');
invokedMeth = invokedMethSb.toString();
/*de.java_chess.javaChess.game.GameImpl$GameStatus*/
StringBuilder has some good utils for these operations, such as setCharAt.
As a personal opinion, I prefer the following one:
char[] invokedArray = invokedMeth.toCharArray();
invokedArray[invokedMeth.lastIndexOf(".")]='$';
invokedMeth = new String(invokedArray);
/*de.java_chess.javaChess.game.GameImpl$GameStatus*/
Regex solution:
You can use the Positive Lookahead, (?=([^.]*)$) where ([^.]*) matches any number of non-dot (i.e. [^.]) character and $ asserts position at the end of a line. You can check regex101.com for more explanation.
public class Main {
public static void main(String[] args) {
String str = "de.java_chess.javaChess.game.GameImpl.GameStatus";
str = str.replaceAll("\\.(?=([^.]*)$)", "\\$");
System.out.println(str);
}
}
Output:
de.java_chess.javaChess.game.GameImpl$GameStatus
A proper regular expression can also help with this replacement:
String withDot = "de.java_chess.javaChess.game.GameImpl.GameStatus";
String with$ = withDot.replaceFirst("(\\w+(\\.\\w+)*)(\\.(\\w+))", "$1\\$$4");
System.out.println(with$);
Output online demo:
de.java_chess.javaChess.game.GameImpl$GameStatus

Regex for NUMERIC(p,s) p=precision and s=scale

Want a regex for NUMERIC(p,s) NUMERIC will always be there and I just want to check whether inside brackets my values are comma separated or not. For example NUMERIC(10,20) so my regex would also contain "NUMERIC(" and comma checking for numbers and ")"
I have tried for comma check but not able to get the NUMERIC with "(" and ")" in my regex.
I have tried with "^NUMERIC\([0-9]+(,[0-9]+)*"
public static void main(String[] args) {
String regex="^NUMERIC\\([0-9]+(,[0-9]+)*"
String v="NUMERIC(1,2)";
System.out.println(v.matches(regex));
}
The expected result is the regex which would give me true for any comma separated number value within "NUMERIC(" and ")"
.matches() expects the entire string to match, and your regex doesn't contain a token for the closing parenthesis.
If there are always two values p and s, you should use something like
public static void main(String[] args) {
String regex="NUMERIC\\([0-9]+,[0-9]+\\)"
String v="NUMERIC(1,2)";
System.out.println(v.matches(regex));
}
If the second parameter is optional:
public static void main(String[] args) {
String regex="NUMERIC\\([0-9]+(?:,[0-9]+)?\\)"
String v="NUMERIC(1)";
System.out.println(v.matches(regex));
}
See also Difference between matches() and find() in Java Regex
Use this regular exp.
^NUMERIC\([0-9]+(,[0-9]\){1})
I just modified yours.

How to check specific special character in String

I am having below String value, in that how can I find the only this four specified special character like [],:,{},-() (square bracket, curly bracket, hyphen and colon) in a given String.
String str = "[1-10],{10-20},dhoni:kholi";
Kindly help me as I am new to Java.
I think you can use regular expression like this.
class MyRegex
{
public static void main (String[] args) throws java.lang.Exception
{
String str = "[1-10],{10-20},dhoni:kholi";
String text = str.replaceAll("[a-zA-Z0-9]",""); // replacing all numbers and alphabets with ""
System.out.print(text); // result string
}
}
Hope this will help you.
If it is only characters that you want to check then you can use String.replaceAll method with regular expression
System.out.println("[Hello {}:-,World]".replaceAll("[^\\]\\[:\\-{}]", ""));

Split numbers and letters from string with regular expression

I have the following regex:
String regx = "\\d{2}\\w{3}";
It says, two numeric and three alphanumeric chars in string. I want to split String by using above regex.
Example:
String stringToSplit = "99E0L";
Output will be
99
E0L
Is it possible to split above String using above regex in Java? What API should I use to do it?
Capturing instead of splitting should be your obvious choice. But if you want to split, then you can use zero-width assertions.
public static void main(String[] args) {
String stringToSplit = "99E0L";
String[] arr = stringToSplit.split("(?<=\\G\\d{2})");
for (String s : arr) {
System.out.println(s);
}
}
O/P:
99
E0L

How to remove a numeric value at specific position in a String

I want to remove a numeric value from a specific position position. I have used a regex but it deletes every numeric value from the String.
I have these Strings:
Draft1(admin)
Draft2(adminn)
Draft21(admin23)
Draft112(admin211)
And I want these strings as:
Draft(admin)
Draft(adminn)
Draft(admin23)
Draft(admin211)
currently I've used regex:
name = name.replaceAll("\\d", "");
which replaces all the numeric values and I get something like:
Draft(admin)
You can simply use String#replaceFirst with regex like (?i)(?<=Draft)\d+ to delete this digits:
name = name.replaceFirst("(?i)(?<=Draft)\\d+","");
Where:
(?i) makes regex caseinsensitive, so the Draft could be even DRAFT or draft
(?<=Draft) is lookbehind for Draft word, which asserts that what immediately precedes the current position in the string is Draft
\\d+ are one or more digit to be replaced
(?<=Draft)\\d+\\b
You can use this and replace by empty string.The lookbehind will make sure it replace only numbers after Draft.
You could try this
class String111
{
public static void main(String args[])
{
String s1="Draft1(admin)";
String s2="Draft21(admin23)";
System.out.println(s1.substring(0,s1.indexOf('(')).replaceAll("\\d", "")+s1.substring(s1.indexOf('('),s1.length()));
System.out.println(s2.substring(0,s2.indexOf('(')).replaceAll("\\d", "")+s2.substring(s2.indexOf('('),s2.length()));
}
}
This works
public static void main(String[] args) {
String name = "Draft112(admin211)";
name = name.replaceAll("\\d+(?=\\()","");
System.out.println(name);
}

Categories

Resources