问题
EDITED
I googled to write a custom regular expression for my web application, and still I can't get what I wanted.
I want to check if a string passes this pattern or not:
*STRING*STRING INCLUDING ALL CHARS*STRING INCLUDING ALL CHARS#
for example:
*STRING*the first string تست یک*the second string تست دو#
Should return TRUE
*sdsdsd*the first string تست یکthe second string تست دو#
should return FALSE(because it's not the pattern as *STRING*STRING*STRING#)
$check = preg_match("THE RULE", $STRING);
I'm asking for THE RULE here, sorry if I asked my question in a wrong way...
回答1:
To check if a string has this pattern or not: *STRING*STRING*STRING#
:
if (preg_match(
'/^ # Start of string
\* # Match *
([^*]*) # Match any number of characters except *
\* # Match *
([^*]*) # Match any number of characters except *
\* # Match *
([^#]*) # Match any number of characters except #
\# # Match #
$ # End of string/x',
$subject, $matches))
Then use
filter_var($matches[1], FILTER_VALIDATE_EMAIL)
to check whether the first group might contain an e-mail address.
回答2:
No need for a regular expression, use filter_var()
:
function checkEmail($str){
$exp = explode('*', $str);
if(filter_var($exp[1], FILTER_VALIDATE_EMAIL) && $exp[2] && $exp[3] && substr($str, strlen($str)-1, strlen($str)) == '#') {
return true;
}
return false;
}
$valid = checkEmail('*example@example.com*the first string تست یک*the second string تست دو#');
来源:https://stackoverflow.com/questions/12563547/regular-expression-for-email-address-in-arabic