How to replace the end of a string? [duplicate] - java

This question already has answers here:
Replacing last character in a String with java
(13 answers)
Closed 3 years ago.
I got a string (URL) which ends with "hl=en", "hl=ru", "hl=it" etc with other languages. I want to replace (always) last 5 symbols to "hl=en". What kind of regex should I use?

Well if you really want to use Regex you could use:
str.replaceAll(".{5}$", "hl=en");
Will get 5 characters followed by a 'end of line/string' character and replace it with the desired string

Related

Replacing special characters in Java String [duplicate]

This question already has answers here:
Alternation operator inside square brackets does not work
(2 answers)
What is the difference between square brackets and parentheses in a regex?
(3 answers)
Closed 2 years ago.
I want to replace all special characters in the string shown below:
String a="Test’‵"
I want to replace ’ and ‵ with dashes (-). I have tried the following:
a=a.replaceAll("[’|‵]", "-");
This generates the following result:
Test------
instead of
Test--
How can I achieve the desired result?
Don't use square brackets, as it represents a set of single characters to match (a character class).
a=a.replaceAll("’|‵", "-");
Demo!

I'm looking for a java regex that will return true if string contains any char more than 3 times [duplicate]

This question already has answers here:
How to split a string with any whitespace chars as delimiters
(13 answers)
Closed 2 years ago.
I'm looking for a java regex that will return true if a string
contains any char[including special char _(underscore) and -(hyphen)]
more than 3 times.
example : Heloooo --->True hellooo --->Flase Heleoeoe---> False
got one \w{0,2}$ working fine for JS but not for JAVA Can anyone help,
thanks in advance?
Your JS regex doesn't seem right, but this (a|[^a])\1{3,} should work.
Link to regex tester: https://regex101.com/r/MJjNhT/1

Java Pattern.matches [duplicate]

This question already has answers here:
Why is String.matches returning false in Java?
(3 answers)
Closed 5 years ago.
Pattern.matches("[A(BC)]", "BC") why this returns false?
Because the pattern expects to see a single character from the class A(BC), and matches matches the entire input against the regex (doesn't look for partial matches). Since the input is two characters, it isn't a match.

Regex find digits after String [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 6 years ago.
From the two urls
https://twitter.com/realDonaldTrump/status/829684271812067328
https://twitter.com/realDonaldTrump/status/829684271812067328/
I want to extract the String 829684271812067328 in Java.
My attempt was
\\/status\\/([\\d]+)
but this does not allow anything before /status or after the digits (/).
Whats the solution to this?
If you just need to extract data (rather than validate the url format), I'd use the following regex :
(\\d+)/?$
And extract the first group of the result.
It matches a non-empty sequence of digits that can be followed by a / and must be found at the end of the matched text.

Replace dashes with underscores within Any type of bracket [duplicate]

This question already has answers here:
How to replace dashes with underscores within the square brackets using regex Java
(5 answers)
Closed 7 years ago.
I want to replace dashes with underscores within any bracket in a string.
Example String:
[a]-[a-gamma]+(a-alpha)*{a}-{b-gamma}+[a]
replaceAll=?
output
[a]-[a_gamma]+(a_alpha)*{a}-{b_gamma}+[a]
Try to do this one using lookbehind mechanism in regexp
String input = "[a]-[a-gamma]+(a-alpha)*{a}-{b-gamma}+[a]";
String result = input.replaceAll("-(?![\\[\\{\\(])","_");

Categories

Resources