I have no idea how to make a regex , that's why i am asking this question.I have one string like chirag patel <chiragxxx#gmail.com>
I have a regex to get email id from the string.
preg_match("/\<(.*)\>/", $data['From'], $matches);
$email = $matches[1];
How to get name from above string using regex?
my expected output is: chirag patel.
You can use the regex
.*(?=\<(.*)\>)
check the demo here. here is the php code for the following
$re = '/.*(?=\<(.*)\>)/';
$str = 'chirag patel <chiragxxx#gmail.com>';
preg_match($re, $str, $matches);
var_dump($matches[0]);
Use this in php
$data['From'] = "chirag patel <chiragxxx#gmail.com>";
preg_match("/.*(?=\<(.*)\>)/", $data['From'], $matches);
print_r($matches); // 0 : name, 1 : email
You add a capturing group for the name.
preg_match("/(.*)\<(.*)\>/", $data['From'], $matches);
$name = $matches[1];
$email = $matches[2];
You may use this regex to capture name and email address in 2 separate groups:
(\pL+[\pL\h.-]*?)\h*<([^>]+)>
RegEx Demo
RegEx Breakup:
(\pL+[\pL\h.-]*?) # group #1 that match 1+ name consisting Unicode letters, dots, hyphens, spaces
\h*: Match 0 or more whitespaces
<([^>]+)>: group #2 to capture email address between < and > characters
Code:
preg_match('~(\pL+(?:[\pL\h-]*\pL)?)\h*<([^>]+)>~u', $str, $matches);
// Print the entire match result
print_r($matches);
I know it has been answered but because it's in PHP, which supports named patterns, and because might look cool:
/(?<name>.*?) \<(?<email>.*?)\>/g
name and email will be keys in the $matches array.
Related
I want to find all the words that contain a specified group of letters, for example if I want to search the with the group of letter ph int the text phone find phill phdas I want the REGEXP to return me phill phone phdas I dont want to do it in another way than REGEXP. (PHP)
You can use this regex:
/\w*ph\w*/
That will match 0 or more word characters, followed by your search term ph, followed by 0 or more word characters.
RegEx Demo
PHP Code:
$kw = 'ph';
preg_match_all('/\w*' . $kw . '\w*/', $str, $matches);
You can use the Pregmatch function of php and as it's first argument give the regex as /\wph\w/. Give stars after the w as well , that is for any character present..
Try this:
<?php
$subject = "your subject";
$pattern = '//\w*ph\w*//';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>
I have a preg_match matching for specific patterns, but it's just not matching the pattern I'm trying to match. What am I doing wrong?
<?php
$string = "tell me about cats";
preg_match("~\b(?:tell me about|you know(?: of| about)?|what do you think(?: of| about)?|(?:what|who) is|(?:whats|whos)) ((?:[a-z]+ ){1,2})$~", $string, $match);
print_r($match);
?>
Expected Result:
array(0 => tell me about 1 => cats)
Actual Result:
array()
You are having an extra space in (but there are no spaces after cat making the entire regex to fail)
((?:[a-z]+ ){1,2})
^^
||
here
also, you don't have capturing group for first part (due to (?:..)). Make a capturing group and make the spaces optional using ? (if you want to capture at most two words)
\b(tell me about|you know(?: of| about)?|what do you think(?: of| about)?|(?:what|who) is|(?:whats|whos)) ((?:[a-z]+){1,2} ?)$
Regex Demo
PHP Code
$string = "tell me about cats";
preg_match("~\b(tell me about|you know(?: of| about)?|what do you think(?: of| about)?|(?:what|who) is|(?:whats|whos)) ((?:[a-z]+ ?){1,2})$~", $string, $match);
print_r($match);
NOTE :- $match[1] and $match[2] will contain your result. $match[0] is reserved for entire match found by the regex in the string.
Ideone Demo
so i want to find usernames in a string and put them in an array, i've made the regex and it returns the match, but when there are 2 matches it only puts the first one in the array. Can anyone see what is wrong with my regex?
$reactie = 'hey #sjerd and #jeska';
$pattern = '/#\w*/';
preg_match($pattern, $reactie, $matches);
print_r($matches);
You need to use preg_match_all with correct regex with word boundary:
$reactie = 'hey #sjerd and #jeska';
$pattern = '/#\w+\b/';
preg_match_all($pattern, $reactie, $matches);
print_r($matches[0]);
I want to convert certain patterns into links and it works fine as far as normal user ids are considered.But now i want to do the same for encrypted ids as well.
Below is my code:(works)
$text = "hi how are you guys???... ##[Sam Thomas:10181] ##[Jack Daniel:11074] ##[Paul Walker:11043] ";
$pattern = "/##\[([^:]*):(\d*)\]/";
$matches = array();
preg_match_all($pattern, $text, $matches);
$output = preg_replace($pattern, "$1", $text);
Now i need to do link the text like:
"hi how are you guys???... ##[Sam Thomas:ZGNjAmD9ac3K] ##[Jack Daniel:ZGNjAmD9ac3K] ##[Paul Walker:ZGNjAmD9ac3K] ";
But this encrypted is not identified by above regular expression...
##\[([^:]*):(.*?)\]
^^
Try this.See demo.Just change \d* to .*? to accept anything or \w* to accept only numbers and letters.or [^\]]* or [0-9a-zA-Z] as well.
https://regex101.com/r/vD5iH9/52
Change your regex to accept numbers and letters as well.
Something like this -
##\[([^:]*):([0-9a-zA-Z]*)\]
^^^^^^^^^^^ Replaced \d
Demo
I need to be able to split a string that contains email's From information. From the string I need to extract $NAME and $EMAIL or whatever is available.
The string can be in the following formats:
"Santa Clause" <santa#example.com>
Santa Clause <santa#example.com>
<santa#example.com>
preg_match('#(?:"(?<name>[^"]+)"|(?<name>.+))?<(?<email>.+)>#U', $string, $matches);
echo var_dump($matches);
preg_match('#(?:"(?<name>[^"]+)"|(?<name>.+))?<(?<email>[^>]+)>#U', $string, $matches);
echo var_dump($matches);
Try one of the above. The former will allow more valid emails, whereas the latter is faster.
$string_to_check = '"Santa Clause" <santa#npole.com>'
$matches = array();
preg_match('/?([^<"]*)"?\s*<(\S*)>/',$string_to_check,$matches);
$matches[1] //=> Santa Claus
$matches[2] //=> santa#npole.com
If the separator is always the same character (e.g. the semicolon):
$items = explode($separator, $from);
Otherwise, browse around in the preg_XXX functions for regex-based string splitting.
For the mail adress, have a look at http://php.net/manual/en/function.preg-match.php. This is a function that matches a string against a regular expression. Here's a short intro into how to use regular expressions with PHP.
If you want to match the name also, it will be some effort, so I suggest you first develop a regular expression that can extract an email address out of your string and then augment it to find the name also.
Found this and it works great!
$parts = preg_split('/[\'"<>]( *[\'"<>])*/', $text, -1, PREG_SPLIT_NO_EMPTY);