Regex patterns for validation - java

I want to use regex pattern for validation,
regex for allowing only alphabets and space only
for this am using ^[a-zA-Z][a-zA-Z\\s]+$ but its not validating correctly.
regex for allowing all aplhabets,numbers except special characters.
Anyone can help me, with this two regex patterns ?
Thanks

^[a-zA-Z\s]+$ (if you want all white spaces, like tabs, etc.)
or ^[a-zA-Z ]+$ (if you want only "normal" spaces)
^[a-zA-Z0-9]+$
http://download.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html

Regex 1 (letters and space only) :
"^[a-zA-Z\\s]+$"
Regex 2 (match all letters, all numbers and underscore)
"^\\w+$"

Related

Regex pattern to allow space

I have one regex which allows one Upper case, one Lower case, 8-16 characters and Most special characters including space. I want add allow space in the regex.
I have tried :
Blank spaces in regular expression
Regex to allow alphanumeric, spaces, some special characters
Java space and newline regex for split
My regex is as follow :
(?=^.{8,16}$)(?=.*[\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\>\=\?\#\[\]\{\}\\\\\^\_\`\~\|])(?=.*[a-z])(?=.*[A-Z])(?!.*\s)[0-9a-zA-Z\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\>\=\?\#\[\]\{\}\\\\\^\_\`\~\|]]*$
I just want to add space in this. I have tried \s and [ ]? but nothing works.
I have checked the regex on https://regex101.com/
Any suggestions would be appreciated.
Probably there will be mistake in grouping.
As far as I have used RegEx, [ ] or \s will support the single space

regular expression to validate 2 alphanumerics

I have the follow pattern to validate a string, it has to validate 4 letters, 6 numbers, 6 letters and 2 alphanumerics, but with my current pattern I cant get a valid test
Pattern.compile("[A-Za-z]{4}\\d{6}\\w{6}\\[A-ZÑa-zñ0-9\\- ]{2}");
I think my pattern it's wrong, because I'm not shure about this [A-ZÑa-zñ0-9\\- ]{2}
Can you please help me?
You can use pattern:
^[a-zA-Z]{4}[0-9]{6}[a-zA-Z]{6}[a-zA-Z0-9]{2}$
Check it live here.
In your expression you are using \w+, which does not only match digits and alphabetic characters, but also underscores _.
A few things off on your regex.
You have extra backslashes in your digit and word matching. Change from \\d to \d and \\w to \w.
The \\ is not needed.
Your end regex is invalid syntax. Just remove the "\\- " bit.
You can also slim down your initial part to be \w instead of [A-Za-z]. So, you're new regex should look like:
"\w{4}\d{6}\w{6}[A-ZÑa-zñ0-9]{2}"
That is if you're okay with the only non-ascii characters being Ñ and ñ in your last two alphanumerics.

How to restrict more than one whitespace in Java Regex?

A validation to be developed for a JavaFX text field where single whitespace is allowed but more than one whitespace is not be allowed.
For example, "Apple Juice" -- correct "Apple Juice" -- incorrect
should be restricted
if (title.matches("[a-zA-Z0-9 ]+"))
Found couple of links but not meeting my requirement. I believe that it is more of a logical tweak.
Whitespace Matching Regex - Java
Regex allowing a space character in Java
You can do:
if (title.matches([a-zA-z]+[ ][a-zA-Z]+))
The first [a-zA-z]+ checks for any characters before the space.
The [ ] checks for exactly one space.
The second [a-zA-z]+ checks for any characters after the space.
Note: This will match only if the space is present in between the string. If you want to match strings like Abcd<space> or <space>Abcd, (I used <spcace> as SO does not allow two spaces to be present simultaneously) then you can replace the +s with *s., i.e.,
if (title.matches([a-zA-z]*[ ][a-zA-Z]*))
You'd better find more than a single whitespace and negate the result:
if(!title.trim().matches("\s{2,}"))
, see java.util.regex.Pattern javadoc for the syntax. The string is first trimmed, so you don't need to check for non-whitespace characters. If you don't do the trim() operation, leading and trailing whitespace will also be considered.
You could do
if ("Apple Juice".matches("\\w+ \\w+")) {
.......

Constructing a specific regex

I want to make a regular expression in Java, with the next criteria:
Length: 10 characters exactly. Not more, not less.
Can accept any character between A-Z (only uppercase letters) and between digits 0-9.
Can accept only one dash character '-' in any position. It cannot accept any other characters, strictly only one dash.
EXAMPLES:
ABCD-12345
F-01234GHK
09-PL89GG5
LJ8U9N3-Y2
PLN86D4V-1
I have been making tries with regex of my own invention, some regular expressions that are close to the result I want, but with no success.
Do I have to combine two regular expressions?
Please, help me to get rid of this issue.... and thanks in advance!!!
I think you need lookahead (which is a way of combining two regular expressions, sort of).
^(?!.*-.*-)[A-Z0-9-]{10}$
The second part will match 10 characters that are A-Z, 0-9, or dash; the first part is negative lookahead that will reject a pattern that has two dashes in it.
You can use this:
^(?![^-]*+-[^-]*+-)[A-Z0-9-]{10}$
Note: If you use the matches method you can remove anchors.

Regular Expression to allow only 0-9 digits only (Not even '.' [Dot]) in JSP using javascript

Could anyone please provide me with a regular expression to allow only digits (0-9) excluding even '.' (decimal point) in a textbox in JSP using javascript. I would be using it to replace the resricted characters with '' (empty string).
I tried few but they are not restricitng the DOT.
Thanks in advance
regex pattern would be:
/^[0-9]+$/
You need to anchor the regex:
/^\d*$/
to make sure that the entire string consists of digits.
Without ^ and $, the regex would match 1 (and 234) in 1.234.
I would use the class \d, it includes only digits. If you want to replace all non-digits you would need to replace \D+ with "".

Categories