Regex to replace everything except trailing digits [duplicate] - java

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

Related

RegEx ignore letters until digit occurs and then match letters afterwards [duplicate]

This question already has answers here:
Using Regular Expressions to Extract a Value in Java
(13 answers)
Closed 3 years ago.
This is intended to be used in Java.
Imagine following sample input:
WRA1007
1085808
1092650S
3901823CV
I want to match all alphabetic characters after at least one digit.
Desired output:
S
CV
Actual output:
0S
3CV
My current approach looks like this:
\d[a-zA-Z]+
The problem with this pattern is that it includes the digit beforehand too. My current solution is to remove the first character of the resulting string afterwards. And this seems quite unsatisfactory to me.
You need a lookbehind:
(?<=\d)[a-zA-Z]+
(?<=\d) means "there must be a digit before this position, but don't match it".
Demo
Alternatively, you can use a pair of () to surround the part you want to get:
\d([a-zA-Z]+)
This is called a "group", and you can get its value by calling group(1) on your Matcher.
If you 'add' groups you can get group 1 that contain only letters
\d([a-zA-Z]+)

Find repeating characters in a string using regex [duplicate]

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.

strip all all of numerics with length less or greater than 6 [duplicate]

This question already has answers here:
Python regex for int with at least 4 digits
(3 answers)
Closed 5 years ago.
Hello all and have a lovely day. I want to ask silly question because i am struggling to find the correct pattern. I want to find all numeric characters which are less or greater than length 6.
Let's take a look below. For instance i have this sequence
12134 4aRt32212121a 11111111111 222222 asda383652re5
My solution is this
\b[0-9]{1,5}\b|\b[0-9]{7,20}\b
What i got as matched is this
12134 11111111111
My problem is that my regex not match any numeric from here 4aRt32212121a. My desired match will be this one
12134 4 32212121 11111111111 5
The numbers will be excluded from match will be 222222,383652 because of the length number equal to 6
i used this Regex online tool to made my tests. you can make the example here if possible.i would appreciate a lot any kind of help and again forgive me for my stupidness
As Eily mentioned in other comment the first issue is \b. This is an anchor for word boundary so it will not match the numbers that are in words like you suggested.
My solution is to remove \b and to make sure you don't get any weirdness add negative lookahead and negative lookbehind and the end and start of your search.
(?<!\d)(\d{1,5}|\d{7,})(?!\d)
edit: accidently typed {1,6} instead of {1,5}
\b means word boundary, ie any place that is not between two \w characters, where \w means numbers, letter or _. In 4aRt32212121a the 3 comes after a letter, so \b\d can't match it. Just remove all your \b.
Edited: And since you don't want to match the 5 digit number inside 111111 you need boundary conditions. With look around assertions you can use:
(?<!\d)(?:[0-9]{1,5}|[0-9]{7,20})(?!\d)
Otherwise (the debugger you have linked to doesn't support them) you have to include either a line boundary or character in the match:
(?:^|[^\d])(?:[0-9]{1,5}|[0-9]{7,20}) (?:$|[^\d])

Valid regex expression? [duplicate]

This question already has answers here:
Regular expression to match standard 10 digit phone number
(23 answers)
Closed 6 years ago.
I have this string:
+1(333) 456-7890
I want to match it with a regular expression. This is what I have now for my regex pattern:
Pattern p1 = Pattern.compile("((\\+{0,}[0-9]{0,3}( |-)?)?)(\\(?[0-9]
{3}\\)?|[0-9]{3}( |-)?)([0-9]{3}( |-)?)([0-9]{4}|[a-zA-Z0-9]{7})");
It is supposed to recognize any phone number pattern with potential dashes or spaces in the middle, that could be at least 10 digits (or letters), with no country code and at most 13 digits with a country code.
My pattern seems to match certain cases and not others such as the one previously stated. I'm really stumped, any help would be appreciated.
Your regex seems overly complex, which is why it probably breaks somewhere. I tried to follow it but in the end it was easier to read your definition and build a new one from scratch:
(\+\d{1,3}[- ]*)?(\(?\d\d\d\)?)[- ]*(\d\d\d[- ]?\d\d\d\d|[a-zA-Z0-9]{7})
This matches the following test cases:
+1(333) 456-7890
+1-(212) 555-0198
+1 212 555-0198
+1 212-ILOVEUU
Depending on your own test cases, this might be enough. Or not.
(\+\d{1,3}[- ]*)? // Optional +xxx international prefix plus dash/space
(\(?\d\d\d\)?)[- ]* // three-digit area code with optional parens
(\d\d\d[- ]?\d\d\d\d| // Either 7 digits, with optional dash
[a-zA-Z0-9]{7}) // or 7 letters/digits

Remove trailing zeros from string java [duplicate]

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".

Categories

Resources