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

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

Related

how To change my regex to reject underscores [duplicate]

This question already has answers here:
Regular Expressions: How to Express \w Without Underscore
(7 answers)
Closed 2 years ago.
Currently have this regex string in my java code:
^[\\w\\-\\ \\#\\.\\/]{0,70}$
It successfully accepts those characters, however it also accepts underscore, how can modify the regex to reject underscore appearing anywhere in the string?
Your regex is:
^[\\w\\-\\ \\#\\.\\/]{0,70}$
It is using \w which is equivalent of [a-zA-Z0-9_], hence it allows underscore also.
You can change your character class to this:
^[-#. a-zA-Z0-9\\/]{0,70}$
Note that space, dot, #, / don't need to be escaped inside [...] and - if placed at first or last position doesn't require escaping either.

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.

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

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

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