Regex for name with space in HTML5 [duplicate] - php

This question already has an answer here:
html5 pattern for first and last name
(1 answer)
Closed 5 years ago.
I tried below pattern for Name field in HTML5, but everytime I am getting error :- "Please match the requested format"
Apart from above pattern, I also gave different pattern like :-
a) pattern="/^[A-Za-z\s]+$/"
b) pattern="/^[A-Za-z]\s[A-Za-z]+$/"
All the three pattern are not working. What I want is simple Firstname Lastname like "Harry Potter".
Please advice.
Thanks in advance.

You need to remove leading and trailing slashes from your pattern because in JavaScript they're indicate that string is actually a regular expression, but in html attribute it is already known to be a regular expression.
Pattern itself will depend on what kind of names you want to accept, your expression will not accept non-latin names, but there is a lot of people with such names. Basically if you want to check for existence of at least 2 words (since name can contain more then 2 words). For example you can use this pattern="\D\S+(\s+\D\S+)+" that will check for existence for at least 2 words separated by whitespace and each word should not start with a digit.

Related

Is it safe to mass replace legacy ASP tags with <?= ?> [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 4 years ago.
I have this RegEx:
('.+')
It has to match character literals like in C. For example, if I have 'a' b 'a' it should match the a's and the ''s around them.
However, it also matches the b also (it should not), probably because it is, strictly speaking, also between ''s.
Here is a screenshot of how it goes wrong (I use this for syntax highlighting):
I'm fairly new to regular expressions. How can I tell the regex not to match this?
It is being greedy and matching the first apostrophe and the last one and everything in between.
This should match anything that isn't an apostrophe.
('[^']+')
Another alternative is to try non-greedy matches.
('.+?')
Have you tried a non-greedy version, e.g. ('.+?')?
There are usually two modes of matching (or two sets of quantifiers), maximal (greedy) and minimal (non-greedy). The first will result in the longest possible match, the latter in the shortest. You can read about it (although in perl context) in the Perl Cookbook (Section 6.15).
Try:
('[^']+')
The ^ means include every character except the ones in the square brackets. This way, it won't match 'a' b 'a' because there's a ' in between, so instead it'll give both instances of 'a'
You need to escape the qutoes:
\'[^\']+\'
Edit: Hmm, we'll I suppose this answer depends on what lang/system you're using.

Handling danish special characters [duplicate]

This question already has answers here:
how to replace special characters with the ones they're based on in PHP?
(8 answers)
Closed 8 years ago.
I am trying to parse a string, split it on what is not a letter or number
$parse_query_arguments = preg_split("/[^a-z0-9]+/i", 'København');
and construct a mysql query.
Even if I skip the preg_split and try to enter the string directly it breaks it into 2 different strings, 'K' and 'benhavn'.
How can I deal with these issues?
If you're using literal characters like a-z then it won't match accented ones. You might want to use the various character classes available to do more generic matching:
/[[:alpha:][:digit]]/
The [:alpha:] set is much broader in scope than a-z. Remember character matching is done based on character code, and a-z in order take, literally, characters between a and z by index. Characters like ø lie outside this range even if they'd fall between that alphabetically.
Computers work in ASCII-abetical (UNICODEical?) order.
This might help explain what is going on in your regex... Regex and Unicode.
You could try something like \p{L} as explained in this question

What does this PHP regular expression mean? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I'm learning PHP regular expressions, and I came across something I'm having trouble making sense of.
The book gives this example in validating an e-mail address.
if (ereg("^[^#]+#([a-z0-9\-]+\.)+[a-z]{2,4}$", $email))
I'm not clear on a couple elements of this expression.
what does this mean [^#]+#
What is the purpose of the parentheses in ([a-z0-9\-]+\.)?
[^#]+# means:
[ - Match this group of characters
^# - Anything that is NOT an at sign
]
+ - One or more times
# - Match an at sign
So, it's essentially matching every character before the first at sign.
The purpose of parenthesis in ([a-z0-9-]+.) is to create a capturing group, which you should be able to reference later on once the group captures some amount of text.
Also note that ereg_* functions are deprecated, and your book must be a bit dated. Nowadays, we use the preg_* family of functions. A tutorial on converting them can be found in this SO question.

How to validate a hyperlink from different links using php

can you please tell me how to validate a hyperlink from different hyperlinks. eg
i want to fetch these links separately starting with the bolded address(between two stars) from a website using simple html dom
1 http://**www.website1.com**/1/2/
2 http://**news.website2.com**/s/d
3 http://**website3.com/news**/gds
i know we can do it using preg_match ;but i am getting a hardtime understanding preg_match.
can anyone give me a preg_match script for these websites validation..
and can you also explain me what this means
preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url)
what are those random looking characters in preg_match? what is the meaning of these characters?
If you want to learn about regular expression, I think you could get a good start on the regular-expressions.info website.
And if you want to use them more, the book Mastering Regular Expressions is a must read.
Edit: here is a simple walkthrough tho:
the first parameter of preg_match is the regexp string. The second is the string you're testing against. A third optionnal one can be used and would be an array inside which everything captured is stored.
the | are used to delimit your regexp and its options. What is between the first one is the regexp, the i at the end is an option (meaning your regexp is case insensitive)
the first ^ is marking where your string you want to match starts
then (s)? mean that you want one or no s character, and you want to "capture it"
[a-z0-9]+ is any number (even 0) of alphanumeric characters
(.[a-z0-9-]+)* is wrong. It should be (\.[a-z0-9-]+)* to capture any number of sequences formed by a dot then at least one alphanumeric character
(:[0-9]+)? will capture one or no sequence formed by : followed by any number. It's used to get the url port
(/.*)? captures the end of the url, a slash followed by any number of any character
$ is the end of your string
Have a look at In search of the perfect URL validation regex.

php regular expression checking for name [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP regex to check a English name
I'm trying to write a regular expression that will check a name field that i have for a user.
I want it to check for
Firstname SPACE Middle initial SPACE Last name.
I know for singular letters for the middle intitial I'll need [a-z] but I'm not sure how to do the full words for first and last name and the spaces in between.
You might want to use str_word_count() to make it a bit easier for your self.
Also, to check for names you might want to add more letters than just a-z, you should take a look at the answer in this question.
Name may contain - symbol. /^[\-a-z]+\s[\-a-z]\s[\-a-z]+$/i
^[A-Za-z]+\s[A-Za-z]\s[A-Za-z]+$

Categories