Java regex in android [duplicate] - java

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

Related

Java REGEX allowing excluded characters [duplicate]

This question already has answers here:
How to match hyphens with Regular Expression?
(6 answers)
Why is this regex allowing a caret?
(3 answers)
Closed 5 years ago.
I have the REGEX below which I am expecting to exclude certain characters. These characters are correctly excluded: £"~#¬|{} but these aren't: #[]/?;:
So, for example, test£test is correctly identified as invalid, but test#test is incorrectly identified as valid.
Testing this on https://regex101.com/ identifies the problem as the brackets and indicates that I need to escape the first ( [bracket] and the - [hyphen] like this - ^[a-zA-z0-9!$%^&*\()\-_=+]+?$. On https://regex101.com the expression then behaves as expected but if I try to use escape characters like this in Java the compiler gives an error.
Any ideas how I can get this regular expression to behave as I want? Sorry if this is obvious.
final String REGEX = "^[a-zA-z0-9!$%^&*()-_=+]+?$";
System.out.println ("Please enter a password");
String password = input.next();
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(password);
if (!m.matches()){
System.out.println("Illegal characters");
Brief
^[a-zA-z0-9!$%^&*()-_=+]+?$
^^^ ^^^
The first underlined range is A-z. This matches:
ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz
The second underlined range corresponds to
)*+,-./0123456789:;<=>?#ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
Code
See regex in use here: Note the regex is only the set for the first example below. This is to show which characters it's actually matching.
Use either of the following
^[a-zA-Z0-9!$%^&*()\-_=+]+?$
^[a-zA-Z0-9!$%^&*()_=+-]+?$
^[\w!$%^&*()=+-]+?$
^[\w!$%&^(-+=-]+?$
The issue with your regex is that it contains special characters which require escaping.
All the characters referenced in this page will require escaping if they are valid in your password.
Pattern docs
Therefore you should use a regex something like the following. I have not thoroughly tested this, however, so please write some thorough unit tests to cover all legitimate possibilities.
"^[a-zA-z0-9!\\$%\\^&\\*\\(\\)\\-_\\=\\+]+?$"
Sorry - I now realise that I have a number of meta-characters which all need escaping. The following REGEX behaves as expected, with double backslashes to escape each meta character:
final String REGEX = "^[a-zA-z0-9\\!\\$%\\^&*\\(\\)\\-_\\=\\+]+?$";
If there is a more elegant way I'd love to hear it!

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

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.

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
"(...)\\.(...)\\.(...)\\.(...)"

Regex for Upper Case Number and 2 special characters [duplicate]

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#-]*$

Finding the character "^" using regex [duplicate]

This question already has an answer here:
Escaping special characters in java regex (not quoting)
(1 answer)
Closed 8 years ago.
I'm trying to define a regex pattern that searches for a caret character, but since ^ is used for negation, I'm not sure how to define the pattern. I'm trying to make the program find a string that is a letter then a caret then a number (as you may have guessed, this is a mathematical term), such as "x^23". This is the line I tried:
String caseFour = "[a-zA-Z]" + "^" + "\\d+";
It's not working. Can anyone help me out?
You need to escape that character also since it is a character of special meaning.
String regex = "[a-zA-Z]\\^\\d+";

Categories

Resources