Regex Expression Errors [duplicate] - php

basically I need a preg_match that ensures that only one space character is allowed between each word (IF there is more than one word).
My existing rule is:
(^[A-Za-z\s]+$)
Thanks.

It depends what you mean by "word", but assuming you mean letters in A-Z or a-z you can try this:
^[a-zA-Z]+( [a-zA-Z]+)*$
Note that \s does not mean the space character - it means any whitespace, including a new line.

/^[\S]+\s?[^\s]?/i
Will search for any word with one space after it and not a space after that one space
/\s?\S+\s/
Will search for any character with whitespace on either side or only on the end.

i think to specify a number of occurences to your match you put i.e{1} {1,5} (to match only 1 or between 1 and 5 inclusive) so something like...
^[A-Za-z]+( {1})$

Related

How to check if string contains specific special characters or starting with a space? [duplicate]

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.

Match 2 or more uppercase characters in entire string

I'm trying to create a pattern in PHP that matches 2 or more upper case characters in a string.
I've tried the following, but it only matches 2 or more upper case characters in a row, not the entire string:
preg_match('/[A-Z]{2,}/', $string);
For example, the string "aBcDe" or "Red Apple" should return true.
You just have to allow other characters between your uppercase letters:
^(?:.*?\p{Lu}){2}
Demo
I used \p{Lu} here to include Unicode characters as well. If you don't want that just use [A-Z] instead like you did in your pattern.
This simply means:
^ from the start of the pattern
(?: group:
.*? match anything, but as few chars as possible
\p{Lu} match an uppercase letter
){2} ... two times
If all you need to do is identify that a string contains at least 2 uppercase characters then you can use the following:
[A-Z].*?[A-Z]
Try it here.
If you need to identify the specific uppercase characters in the string then things get more complicated.
UPDATE: As Lucas mentioned, you need a different regex if you want unicode support.
\p{Lu}.*?\p{Lu}
^.*[A-Z].*[A-Z].*$
A simple pattern stating the same would do.See demo.
https://regex101.com/r/pT4tM5/23
[A-Z].*[A-Z]
is about as simple as it gets - match an uppercase followed by anything repeated any number of times followed by any other uppercase letter.
If you need to match the whole line/string that has at least 2 upper case letters, you can also use
^(?=(?:.*[A-Z]){2}).+$
Demo here.

Allowing dash in string but not several consecutive dashes

This is a regex where string must start and end with an alphanumeric character and can contain alphanumeric characters and dashes.
/^[a-zA-Z0-9]{1}[a-zA-Z0-9\-]+[a-zA-Z0-9]{1}$/
How can I make sure that the consecutive dashes are not allowed? for example:
should allow: some-string
should NOT allow: some--string
Thanks
Edit: I want to allow several dashes, just not consecutively. for example "some-thing-here" is OK, and "some--thing" is NOT.
No need for complicated patterns with optional dashes just use this:
/^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$/
See it here on Regexr
Start with at least one alphanumeric. Then there can be a dash followed by at least one alphanumerics 0 ore more times.
Something like:
/^[a-zA-Z0-9]{1}\-?([a-zA-Z0-9]+\-?)*[a-zA-Z0-9]{1}$/
The key part being \-?([a-zA-Z0-9]+\-?)* which makes it read "a letter/number, optional dash, any amount of (some letters/numbers, optionally followed by a dash), ending in a letter or number."
This allows some-string, my-double-dash-string but not my-double--dash-string.
/^[a-zA-Z0-9]+-?[a-zA-Z0-9]+$/
? for "occuring zero or one time"
To allow multiple pieces separated with one dash, change it to:
/^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$/
The (...)* says that the last group (starting with a -) may be repeated zero or more times.
/^[a-zA-Z0-9]+-?[a-zA-Z0-9]+$/
Should work I believe. You don't need to differentiate the initial and ending group of the alphanumberical characters. Simply expect each dash to be surronded by such character.

PHP preg_match needed to ensure only ONE space character is allowed between words

basically I need a preg_match that ensures that only one space character is allowed between each word (IF there is more than one word).
My existing rule is:
(^[A-Za-z\s]+$)
Thanks.
It depends what you mean by "word", but assuming you mean letters in A-Z or a-z you can try this:
^[a-zA-Z]+( [a-zA-Z]+)*$
Note that \s does not mean the space character - it means any whitespace, including a new line.
/^[\S]+\s?[^\s]?/i
Will search for any word with one space after it and not a space after that one space
/\s?\S+\s/
Will search for any character with whitespace on either side or only on the end.
i think to specify a number of occurences to your match you put i.e{1} {1,5} (to match only 1 or between 1 and 5 inclusive) so something like...
^[A-Za-z]+( {1})$

Regular expression for validating a username?

I'm still kinda new to using Regular Expressions, so here's my plight. I have some rules for acceptable usernames and I'm trying to make an expression for them.
Here they are:
1-15 Characters
a-z, A-Z, 0-9, and spaces are acceptable
Must begin with a-z or A-Z
Cannot end in a space
Cannot contain two spaces in a row
This is as far as I've gotten with it.
/^[a-zA-Z]{1}([a-zA-Z0-9]|\s(?!\s)){0,14}[^\s]$/
It works, for the most part, but doesn't match a single character such as "a".
Can anyone help me out here? I'm using PCRE in PHP if that makes any difference.
Try this:
/^(?=.{1,15}$)[a-zA-Z][a-zA-Z0-9]*(?: [a-zA-Z0-9]+)*$/
The look-ahead assertion (?=.{1,15}$) checks the length and the rest checks the structure:
[a-zA-Z] ensures that the first character is an alphabetic character;
[a-zA-Z0-9]* allows any number of following alphanumeric characters;
(?: [a-zA-Z0-9]+)* allows any number of sequences of a single space (not \s that allows any whitespace character) that must be followed by at least one alphanumeric character (see PCRE subpatterns for the syntax of (?:…)).
You could also remove the look-ahead assertion and check the length with strlen.
make everything after your first character optional
^[a-zA-Z]?([a-zA-Z0-9]|\s(?!\s)){0,14}[^\s]$
The main problem of your regexp is that it needs at least two characters two have a match :
one for the [a-zA-Z]{1} part
one for the [^\s] part
Beside this problem, I see some parts of your regexp that could be improved :
The [^\s] class will match any character, except spaces : a dot or semi-colon will be accepted, try to use the [a-zA-Z0-9] class here to ensure the character is a correct one.
You can delete the {1} part at the beginning, as the regexp will match exactly one character by default

Categories