Regex pattern to allow space - java

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

Related

RegEx matching $1 tokens [duplicate]

Imagine you are trying to pattern match "stackoverflow".
You want the following:
this is stackoverflow and it rocks [MATCH]
stackoverflow is the best [MATCH]
i love stackoverflow [MATCH]
typostackoverflow rules [NO MATCH]
i love stackoverflowtypo [NO MATCH]
I know how to parse out stackoverflow if it has spaces on both sites using:
/\s(stackoverflow)\s/
Same with if its at the start or end of a string:
/^(stackoverflow)\s/
/\s(stackoverflow)$/
But how do you specify "space or end of string" and "space or start of string" using a regular expression?
You can use any of the following:
\b #A word break and will work for both spaces and end of lines.
(^|\s) #the | means or. () is a capturing group.
/\b(stackoverflow)\b/
Also, if you don't want to include the space in your match, you can use lookbehind/aheads.
(?<=\s|^) #to look behind the match
(stackoverflow) #the string you want. () optional
(?=\s|$) #to look ahead.
(^|\s) would match space or start of string and ($|\s) for space or end of string. Together it's:
(^|\s)stackoverflow($|\s)
Here's what I would use:
(?<!\S)stackoverflow(?!\S)
In other words, match "stackoverflow" if it's not preceded by a non-whitespace character and not followed by a non-whitespace character.
This is neater (IMO) than the "space-or-anchor" approach, and it doesn't assume the string starts and ends with word characters like the \b approach does.
\b matches at word boundaries (without actually matching any characters), so the following should do what you want:
\bstackoverflow\b

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+")) {
.......

How to match tab and newline but not space with REGEX?

I am trying to match "tab" and "newline" meta chars but without "spaces" with REGEX in Java.
\s matches evrything i.e. tab, space and new line... But, I don't want "space" to be matched.
How do I do that?
Thanks.
One way to do it is:
[^\\S ]
The negated character class makes this regex to match anything except - \\S (non-whitespace) and " "(space) character. So, it will match \\s except space.
Explicitly list them inside [...] (set of characters):
"[\\t\\n\\r\\f\\v]"

Regex to accept only alphabets and spaces and disallowing spaces at the beginning and the end of the string

I have the following requirements for validating an input field:
It should only contain alphabets and spaces between the alphabets.
It cannot contain spaces at the beginning or end of the string.
It cannot contain any other special character.
I am using following regex for this:
^(?!\s*$)[-a-zA-Z ]*$
But this is allowing spaces at the beginning. Any help is appreciated.
For me the only logical way to do this is:
^\p{L}+(?: \p{L}+)*$
At the start of the string there must be at least one letter. (I replaced your [a-zA-Z] by the Unicode code property for letters \p{L}). Then there can be a space followed by at least one letter, this part can be repeated.
\p{L}: any kind of letter from any language. See regular-expressions.info
The problem in your expression ^(?!\s*$) is, that lookahead will fail, if there is only whitespace till the end of the string. If you want to disallow leading whitespace, just remove the end of string anchor inside the lookahead ==> ^(?!\s)[-a-zA-Z ]*$. But this still allows the string to end with whitespace. To avoid this look back at the end of the string ^(?!\s)[-a-zA-Z ]*(?<!\s)$. But I think for this task a look around is not needed.
This should work if you use it with String.matches method. I assume you want English alphabet.
"[a-zA-Z]+(\\s+[a-zA-Z]+)*"
Note that \s will allow all kinds of whitespace characters. In Java, it would be equivalent to
[ \t\n\x0B\f\r]
Which includes horizontal tab (09), line feed (10), carriage return (13), form feed (12), backspace (08), space (32).
If you want to specifically allow only space (32):
"[a-zA-Z]+( +[a-zA-Z]+)*"
You can further optimize the regex above by making the capturing group ( +[a-zA-Z]+) non-capturing (with String.matches you are not going to be able to get the words individually anyway). It is also possible to change the quantifiers to make them possessive, since there is no point in backtracking here.
"[a-zA-Z]++(?: ++[a-zA-Z]++)*+"
Try this:
^(((?<!^)\s(?!$)|[-a-zA-Z])*)$
This expression uses negative lookahead and negative lookbehind to disallow spaces at the beginning or at the end of the string, and requiring the match of the entire string.
I think the problem is there's a ? before the negation of white spaces, which means it is optional
This should work:
[a-zA-Z]{1}([a-zA-Z\s]*[a-zA-Z]{1})?
at least one sequence of letters, then optional string with spaces but always ends with letters
I don't know if words in your accepted string can be seperated by more then one space. If they can:
^[a-zA-Z]+(( )+[a-zA-z]+)*$
If can't:
^[a-zA-Z]+( [a-zA-z]+)*$
String must start with letter (or few letters), not space.
String can contain few words, but every word beside first must have space before it.
Hope I helped.

Regex patterns for validation

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+$"

Categories