Regex take everything after word and before character in PHP - php

I'm trying to get regex to work to take everything after "test" and before "#" in an email so "test-12345#example.com would become 12345.
I've got this far to get it to return everything before the "#" symbol. (Working in PHP)
!(\d+)#!

Either you can use capturing groups and use the regex
test-(\d+)#
and use $1 or use lookaheads and behinds like (?<=test-)\d+(?=#) which will just match 12345

(?<=test-)[^#]+
You can try this.No need to use groups.See demo.
https://regex101.com/r/eZ0yP4/28

You want everything between test and # so don't use \d.
$myRegexPattern = '#test([^#])*##Ui';
preg_match ($myRegexPattern, $input, $matches);
$whatYouNeed = $matches[1];

Try this
$input = 'test-12345#example.com';
$regexPattern = '/^test(.*?)\#/';
preg_match ($regexPattern, $input, $matches);
$whatYouNeed = $matches[1];
var_dump($whatYouNeed);

Related

PHP exploding url from text, possible?

i need to explode youtube url from this line:
[embed]https://www.youtube.com/watch?v=L3HQMbQAWRc[/embed]
It is possible? I need to delete [embed] & [/embed].
preg_match is what you need.
<?php
$str = "[embed]https://www.youtube.com/watch?v=L3HQMbQAWRc[/embed]";
preg_match("/\[embed\](.*)\[\/embed\]/", $str, $matches);
echo $matches[1]; //https://www.youtube.com/watch?v=L3HQMbQAWRc
$string = '[embed]https://www.youtube.com/watch?v=L3HQMbQAWRc[/embed]';
$string = str_replace(['[embed]', '[/embed]'], '', $string);
See str_replace
why not use str_replace? :) Quick & Easy
http://php.net/manual/de/function.str-replace.php
Just for good measure, you can also use positive lookbehind's and lookahead's in your regular expressions:
(?<=\[embed\])(.*)(?=\[\/embed\])
You'd use it like this:
$string = "[embed]https://www.youtube.com/watch?v=L3HQMbQAWRc[/embed]";
$pattern = '/(?<=\[embed\])(.*)(?=\[\/embed\])/';
preg_match($pattern, $string, $matches);
echo $match[1];
Here is an explanation of the regex:
(?<=\[embed\]) is a Positive Lookbehind - matches something that follows something else.
(.*) is a Capturing Group - . matches any character (except a newline) with the Quantifier: * which provides matches between zero and unlimited times, as many times as possible. This is what is matched between the groups prior to and after. This are the droids you're looking for.
(?=\[\/embed\]) is a Positive Lookahead - matches things that come before it.

preg_replace everything but # sign

I've searched for an example of this, but can't seem to find it.
I'm looking to replace everything for a string but the #texthere
$Input = this is #cool isn't it?
$Output = #cool
I can remove the #cool using preg_replace("/#(\w+)/", "", $Input); but can't figure out how to do the opposite
You could match #\w+ and then replace the original string. Or, if you need to use preg_replace, you should be able to replace everything with the first capture group:
$output = preg_replace('/.*(#\w+).*/', '\1', $input);
Solution using preg_match (I assume this will perform better):
$matches = array();
preg_match('/#\w+/', $input, $matches);
$output = $matches[0];
Both patterns above do not address the issue how to handle inputs which match multiple times, such as this is #cool and #awesome, right?

How to use preg_match to extract data?

I am pretty new to the use of preg_match. Searched a lot for an answer before posting this question. Found a lot of posts to get data based on youtube ID etc. But nothing as per my needs. If its silly question, please forgive me.
I need to get the ID from a string with preg_match. the string is in the format
[#1234] Subject
How can I extract only "1234" from the string?
One solution is:
\[#(\d+)\]
This matches the left square bracket and pound sign [#, then captures one or more digits, then the closing right square bracket ].
You would use it like:
preg_match( '/\[#(\d+)\]/', '[#1234] Subject', $matches);
echo $matches[1]; // 1234
You can see it working in this demo.
You can try this:
preg_match('~(?<=\[#)\d+(?=])~', $txt, $match);
(?<=..) is a lookbehind (only a check)
(?=..) is a lookahead
Your regular expression:
preg_match('/^\[\#([0-9]+)\].+/i', $string, $array);
That's a way you could do it:
<?php
$subject = "[#1234] Subject";
$pattern = '/^\[\#([0-9]+)/';
preg_match($pattern, $subject, $matches);
echo $matches[1]; // 1234
?>
To get only the integer you can use subpatterns http://php.net/manual/en/regexp.reference.subpatterns.php
$string="[#1234] Subject";
$pattern="/\[#(?P<my_id>\d+)](.*?)/s";
preg_match($pattern,$string,$match);
echo $match['my_id'];

Can't get PHP Regex working

I'm trying to use PHP regular expressions. I've tried this code:
$regex = "c:(.+),";
$input = "otherStuff094322f98c:THIS,OtherStuffHeree129j12dls";
$match = Array();
preg_match_all($regex, $input, $match);
It should return a sub-string THIS ("c" and ":" followed by any character combination followed by ",") from $input. But it returns a empty array. What am I doing wrong?
I think you need the slashes to make regex working.
and using .+ will match everything behind the comma too, which is you don't want. Use .+? or [^,]+
$regex = "/c:(.+?),/";
or
$regex = "/c:([^,]+),/";

preg_match_all ignore words

I try to create a regex to capture emails ending not .info/.con containing no aaa/bbb.
Is this the correct syntax?
Eg: // search email ending in .com/.info containing no aaa/bbb
preg_match_all('#((?=.*#.*(?:com|info))(!.*(?:aaa|bbb)).*)#ui', $html, $emails);
To get this:
caaac#ccc.com = no
ccc#ccbbb.com = no
cccc#cccc.com = good (address syntax correct + term absent before or after the #)
Thank you for your reply.
This syntax works fine SEE HERE (thank you to STEMA) except for a string that includes spaces.
e.g:
$string = "email1#address.com blah email2#aaaaess.com blah email3#address.info embbbil4#adress.com";
preg_match_all("#^(?!.*aaa)(?!.*bbb).*#.*\.(?:com|info)$#im", $string, $matches);
Cordially
Simply use a positive expression and check that it did not match anything.
if (preg_match(...) == 0)
Also, there is no need to use preg_match_all if you are just interested whether a pattern matched or not.
If I understand your requirements right, then this would be the regex you can use together with #Tomalak answer.
preg_match('#.*#.*(?:aaa|bbb)|\.(?:com|info)$#ui', $html, $emails);
This pattern matches the stuff you don't want.
.*#.*(?:aaa|bbb) matches aaa or bbb after the #
the \.(?:com|info)$ is the other part, this matches if your email address ends with .com or .info
You can see it online here on Regexr
Update:
.*(?:aaa|bbb).*\.(?:com|info)$
This will match aaa or bbb and the string has to end with .com or .info
See it online here on Regexr
Here's the solution:
#(?<=^|\s)(?![\w#]*(?:aaa|bbb|(?:[0-9].*){3,}))[a-z0-9-_.]*#[a-z0-9-_.]*\.(?:com|net|org|info|biz)(?=\s|$)#im
Function:
function get_emails($str){
preg_match_all('#(?<=^|\s)(?![\w#]*(?:aaa|bbb|(?:[0-9].*){3,}))[a-z0-9-_.]*#[a-z0-9-_.]*\.(?:com|net|org|info|biz)(?=\s|$)#im', $str, $output);
if(is_array($output[0]) && count($output[0])>0) {
return array_unique($output[0]);
}
}
Cordially

Categories