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+")) {
.......
Related
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
I need to include space between each string repetition, that is working fine using )-- )+ at the end of the regex, but I need to delete the space for the last string entered.
Using the following regex the space is mandatory for all of the strings including the last one which is not necessary.
How to avoid the space to be mandatory for the last repeated string?
This is my regex:
"(?:sv(?:32i|32e)(?:a|m|c|)-(?:32f|32e)(?:a|b|)-- )+"
You could match the first part sv32[ie][amc]-32[ef][ab]-- without the trailing space and repeat matching the same pattern preceded by a space in a non capturing group (?: ..)* or use + to repeat 1 or more times.
The pattern might be simplified a bit using character classes instead of the non capturing groups.
sv32[ie][amc]-?32[ef][ab]--(?: sv32[ie][amc]-?32[ef][ab]--)*
Regex demo
Note that if you want to match sv32ia-32ea-- sv32ia32ea-- sv32ia-32ea-- sv32ia32ea-- you could make the hypen optional using -?32
You could also make the space optional using -- ?)+ if you would allow a trailing 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
I want to match just two strings before a matched string
e.g Rohan pillai J.
Currently i am using :
pattern= (?=\w+ J[.])\w+
Answer - pillai
desired answer - Rohan pillai
An alternative to take the first two names:
\w*\s\w*(?=\sJ\.)
Regex live here.
Explaining:
\w*\s # the first word (name) followed by space
\w* # the second word (name)
(?=\sJ\.) # must end with space and "J." - without taking it
Tip: Generally to escape regex metacharacters (like dot .) we use back-slash. Use character class like [.] if you want to put emphasis on that character (if you want to make it more visible when you will read this regex).
You need to put the look ahead in trailing :
(\w+) (\w+)(?= J\.)
See demo https://regex101.com/r/wH0oU8/1
Or more general you can use \s to match any whitespace instead of space :
(\w+)\s(\w+)(?=\sJ\.)
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.