Replacing one character in string with another string excluding pattern [duplicate] - java

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 5 years ago.
I need to replace all '%' characters in string with "%25" but I don't want to replace % in %25 to avoid situation, when I get something like this %%25.
I want to do it in Java.
Example:
input: % sdfsdaf %25
expected result: %25 sdfsdaf %25
Do you know what should I use to get it?

Without discussing much and digging deep into what you are really trying to accomplish with the replacement ... the answer to your question could be something like :
myString.replaceAll("%(?!25)", "%25");
From Pattern Documentation ; The (?!...) part means "only match if the text following (hence: lookahead) this doesn't (hence: negative) match this. But it doesn't actually consume the characters it matches (hence: zero-width).
Section 3.5 of the Link clarifies it in a bit more detail.

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.

Hyphen character doesn't seem to work in Regex expression in Java [duplicate]

This question already has answers here:
Regex - Should hyphens be escaped? [duplicate]
(3 answers)
Closed 5 years ago.
I've tried different ways to make the special character '-' work, but it doesn't seem to work when I try to test the regex within the code. The examples I tried were these
[^(a-zA-Z0-9\\\\##$%!.'{}_-~`())]
The above one would not work as it searches for characters between '_' to '~'. Then I tried with
[^(a-zA-Z0-9\\\\##$%!.'{}_~`()\\-)]
[^(a-zA-Z0-9-\\\\##$%!.'{}_~`())]
[^(a-zA-Z0-9\\-\\\\##$%!.'{}_~`())]
None of the above seem to be working if I give '-' in the string to search. The above expressions work when I try to test in regex tester online.
EXTERNAL_USER_INVALID_PATTERN = "[^(a-zA-Z0-9\\\\##$%!.'{}_~`()\\-)]"
Pattern p = Pattern.compile(X.EXTERNAL_USER_INVALID_PATTERN);
if(p.matcher(objectName).find() || objectName.length()> X.EXTERNAL_USER_MAXLENGTH){
throw new BusinessException("The group name does not conform to specification");
}
It always throws the given exception. All the other combinations without '-' seems to be working.
Hyphen characters have special meaning inside character classes. To include it, put it as first or as last character in the class. E.g. [-A-Z] or [^A-Z-].

This pattern matches for input 123456789.2.2.2 , which it should not [duplicate]

This question already has answers here:
Java RegEx meta character (.) and ordinary dot?
(9 answers)
Closed 6 years ago.
I am trying to solve following task:
Match the pattern abc.def.ghi.jkl, where each variable a,b,c,d,e,f,g,h,i,j,k,l can be any single character except the newline.
For above question I am matching the input to regex :
"([^\\n]{3}(.)){3}([^\\n]{3})"
// this is the regex pattern I am using currently
What am I doing wrong? Please help me correct the above regex so that it does not match the incorrect input I have provided in the title. Currently it matches to it somehow. Although I have provided 3 it is apparently matching to more than 3 characters.
. has a special meaning in regular expression patterns.
If you want to get a "simple dot", you need to quote/escape it (as "\\.").
And that special meaning is (under normal configuration) "any character except line breaks", which exactly matches your other condition, so you can simplify this to
"(...)\\.(...)\\.(...)\\.(...)"

Java regex in android [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 7 years ago.
if someone could help me with my regex-problem, it would be aweful !
I have an EditText and the charakters which are allowed to be typed in are the following:
A-Z, Ä, Ö, Ü (only uppercases), and ß
I'm grateful for any help!
Use [A-ZÄÖÜß]+ as your regular expression.
Replace + with * if you want to support zero length strings.
(You might want to use \u notation in your source code in place of the special characters to help code editors and source control systems.)
You mean something like this?
Pattern p = Pattern.compile("^[A-ZÄÖÜß]+$");
Matcher m = p.matcher(inputString);
if(m.matches() {
//Do whatever you need to do when the pattern matches.
}

Categories

Resources