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.
Related
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("[^\\]\\[:\\-{}]", ""));
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);
}
I'd like to replace all occurrences of 2 consecutive commas (",,") by a marker in between, but I've found that I can't replace the second occurrence. The code is as follows:
String addresses = "a,,,b";
String b = addresses.replace(",,", ",EMPTYADDR,");
System.out.println(b);
I expect the result to be:
a,EMPTYADDR,EMPTYADDR,b
But instead, I get:
a,EMPTYADDR,,b
How should I change the code to get the desired result?
Pass lookaround based regex in replaceAll function. Lookarounds won't consume any character but asserts whether a match is possible or not.
string.replaceAll("(?<=,)(?=,)", "EMPTYADDR");
DEMO
(?<=,) tells the regex engine to lookafter to all the commas.
(?=,) tells the regex engine to match all the boundaries which exists after all the commas only if it's followed by another comma.
So two boundaries are being matched. By replacing the matched boundaries with EMPTYADDR will give you the desired output.
Simple non-Regex method using a while loop
public static void main(String[] args) {
String addresses = "a,,,b";
while (addresses.contains(",,")){
addresses = addresses.replace(",,", ",EMPTYADDR,");
}
System.out.println(addresses);
}
Results:
a,EMPTYADDR,EMPTYADDR,b
You could also split the string, fill in the empty elements, and then reconstruct with String.join()
public static void main(String[] args) {
String addresses = "a,,,b";
String[] pieces = addresses.split(",");
for (int i = 0; i < pieces.length; i++) {
if (pieces[i].isEmpty()) {
pieces[i] = "EMPTYADDR";
}
}
addresses = String.join(",", pieces);
System.out.println(addresses);
}
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 ""
Am I misinterpreting something regarding Java regexes? Shouldn't the following match the leading zero:
public class Testit {
public static void main(String[] args) {
format("0115724848");
}
private static void format(String elementToFormat) {
if (elementToFormat.matches("^0")) {
System.out.println("leading zero:" + elementToFormat);
} else {
System.out.println("no leading zero:" + elementToFormat);
}
}
}
matches tries to match the pattern against the whole of the input string... and your input string isn't just "beginning of string followed by 0".
Either you need "0.*" (the ^ is unnecessary precisely because matches will match the whole string) or you could create a Pattern and then use:
if (pattern.matcher(text).lookingAt())
Of course it's not clear why you're using a regex here at all, in that you can use:
if (text.startsWith("0"))
String.match wants to match the whole String, and your regex ^0 doesn't.
Instead you need a regex like: 0.*, which means a "the string begins with 0, followed by zero or more characters". Or, depending on your needs, 0\d*, which means "the string begins with 0 followed by zero or more digits", which is what your example input looks like.
if ("0115724848".matches("0\\d*"))
System.out.println("leading zero.");
This is the pattern that you should use:
^0.*
Also why not use startsWith("0") - much simpler
Check this code..It should work.
public class Testit {
public static void main(String[] args) {
format("0115724848");
}
private static void format(String elementToFormat) {
if (elementToFormat.matches("^0.*")) {
System.out.println("leading zero:" + elementToFormat);
} else {
System.out.println("no leading zero:" + elementToFormat);
}
}
}
This regex will match numbers with leading zeros, but not "0": /^0+[1-9]/