This question already has answers here:
Regex for removing trailing zeros
(5 answers)
Closed 7 years ago.
Although I have seen a question similar to this one asked quite a few times, I actually mean remove all trailing zeroes.
I would like to convert something like
"1903895810000"
to
"190389581"
I am looking for a String.replace() solution
Simple regexp with replaceAll will do it.
String s = "1903895810000";
System.out.println(s.replaceAll("0+$", ""));
[EDIT]:
s.replace(0, "") will not work here, because it will remove all zeros from the string, so you can't use it. So, here I used replaceAll, that uses regular expressions to match replacement string. This simple regexp 0+$ matches any number of zeros 0+ followed by end-of-string $, so it would be "some zeroes at the end".
Related
This question already has answers here:
Trim leading or trailing characters from a string?
(6 answers)
Closed 2 years ago.
I'd like to trim any single trailing . or -. I tried doing this by doing something like "f-o.o.".replaceFirst("^(\\.+)[-|.]$", "$0"). The expected string is f-o.o but I'm getting f-o.o.. Thank you.
Your expression has two mistakes:
you put a slash in front of a dot, making it match a literal dot, not just any character
you put | into a character class, so your expression would remove not just . or - at the end of the string, but also |.
Use "f-o.o.".replaceFirst("[-.]$", "")
This question already has answers here:
Regular expression to match any character being repeated more than 10 times
(8 answers)
Closed 4 years ago.
I'm trying to find a regex which finds 3 repeating characters appearing contiguously in a string. The character set could be alphabet, digit or any special character.
I wanted the first try for alphabet and digits and then extend the expression to include special characters. The ones I tried are. Both of these fail for the string "c2sssFg1". What am I doing wrong here?
(\\w*)\\2{3,}(\\w*)
(\\w*?)(\\w)\\2{3,}(\\w*)
I looked at some of the examples on SO and on web but I didn't find the right solution that passes the random strings I test.
Can someone help me with this?
Thanks.
(.)\1{2}
(.) matches any char
\1 matches that exactly char
{2} is to grant its 2 more of that
Try (.)\1\1. It works for general case.
This question already has answers here:
Get the Integer from the end of a string (variable length)
(10 answers)
Closed 5 years ago.
This should be pretty straight-forward, but I'm unable to find the answer nor come up with it. I have a String which always has one or more trailing digits, and potentially other digits elsewhere. I want to remove everything from the string except for all trailing digits.
Some example test cases:
"1 test50" -> "50"
"anothertest10" -> "10"
"can contain spaces123" -> "123"
"ok1" -> "1"
I tried the most obvious thing: str.replaceAll(".*(\\d)+$","$1"), but unfortunately the .* is matched first, so for the test cases above it will result in 0,0,3,1 instead of 50,10,123,1.
I have the feeling I need to use a look-behind or something, although I've barely used them in the past and I'm not sure how to apply it to my problem.
you can use the regex
\d+$
which would match all trailing digits, see the regex101 demo
This question already has answers here:
How do I use a delimiter with Scanner.useDelimiter in Java?
(3 answers)
Closed 5 years ago.
I want the scanner to ignore three things: empty spaces, "/" and "!".
What is the correct argument to use in the useDelimiter method?
useDelimiter takes a regex argument docs:
pattern - A string specifying a delimiting pattern
So just make sure the string is in regex form.
Whitespace in regex is \s, escape that to become \\s. / is still / and ! is still !. You then use | to act as an "or" operator to say "either one of these".
Here's how to do it:
scanner.useDelimiter("\\s|/|!");
If you want to say that "consecutive whitespaces slashes and exclamation marks also count as delimiter", then you can add a quantifier + to the whole thing:
scanner.useDelimiter("(\\s|/|!)+");
Scanner's delimiter is just a pattern, so you could use the following:
sc.useDelimiter("[\\s/!]*");
This question already has answers here:
Regex to accept alphanumeric and some special character in Javascript? [closed]
(2 answers)
Closed 7 years ago.
I would need a Regex which allows only uppercase letters, number and two special characters.
so far I have the Regex for the letters and numbers.
^[A-Z0-9]+$
I would also like to allow the usage of the "-" and "#" symbols
like
A453#
A-59#
for example
Any advices?
Cheers
^[A-Z0-9#-]+$
This should do it for you.
Often numbers must start form a letter (e.g. "A1234-5" is allowed when "###" is ruled out), if it's your case, the pattern is
^[A-Z][A-Z0-9#-]*$