I'm looking for an universal regular expression which extracts the twitter username from an url.
Sample URLS
http://www.twitter.com/#!/donttrythis
http://twitter.com/KimKardashian
http://www.twitter.com/#!/KourtneyKardash/following
http://twitter.com/#!/jasonterry31/lists/memberships
There are a couple more test cases to make a universal regexp.
https URLs are also valid
URLs like twitter.com/#username also go to username's profile
This should do the trick in PHP
preg_match("|https?://(www\.)?twitter\.com/(#!/)?#?([^/]*)|", $twitterUrl, $matches);
If preg_match returns 1 (a match) then the result is on $matches[3]
Try this:
^https?://(www\.)?twitter\.com/(#!/)?(?<name>[^/]+)(/\w+)*$
The sub group "name" will contain the twitter username.
This regex assumes that each URL is on its own line.
To use it in JS, use this:
^https?://(www\.)?twitter\.com/(#!/)?([^/]+)(/\w+)*$
The result is in the sub group $3.
this regex works fine in jQuery
$('#inputTwitter').blur(function() {
var twitterUserName = $(this).val();
$(this).val(twitterUserName.match(/https?:\/\/(www\.)?twitter\.com\/(#!\/)?#?([^\/]*)/)[3])
});
This one is based on Lombo's answer, works without http(s) too, is less hungry (not keeping spaces after the username) and returns first in the result.
Check it in action: https://regex101.com/r/xI2vF3/3
For js:
(?:https?:\/\/)?(?:www\.)?twitter\.com\/(?:#!\/)?#?([^\/\?\s]*)
Lombo's answer is my favorite, but it will glom any query string in with the result:
http://www.twitter.com/#!/donttrythis?source=internet
will result in a username of "donttrythis?source=internet"
I'd modify it to be:
preg_match("|https?://(www\.)?twitter\.com/(#!/)?#?([^/\?]*)|", $twitterUrl, $matches);
Adding \? to the excluded character class after the username ensures the query string is excluded.
This regex matches all four given URLs. The user name is present in $1
m[twitter\.com/+(?:#!/+)?(\w+)]
Use this to check
perl -le '$_="<url>"; m[twitter\.com/+(?:#!/+)?(\w+)]; print $1'
This one works for me (in PHP): /twitter\.com(?:\/\#!)?\/(\w+)/i
I found Lombo's answer to work the best except it would not work if the URL was www.twitter.com/example . The following works for me on www as well.
$dirty_twitter = array( 'https://twitter.com/', 'http://twitter.com/', 'www.twitter.com/', 'https://www.twitter.com/', 'http://www.twitter.com/', 'twitter.com/' );
$clean_twitter = str_replace( $dirty_twitter, '', $clean_twitter );
Related
I need to remove part of a URL with a regex.
From the words: http or https to the word .com.
And it can be several times in one string.
Can anyone help me with this?
For example a string:
"The request is:https://stackoverflow.com/questions"
After the removal - "The request is:/questions"
The regex that performed the deletion perfectly is: (#"\w+://[^/$]*")
with replace "".
Something like that:
var regex = new Regex(#"\w+:\/\/[^\/$]*");
regex.Replace(url, "");
You can use the re.sub() function from the regex package. Alternatively if your working with python you can use urlparse package to extract different parts of the url and concatenate it to the prefix you want.
I'm having trouble trying to learn how to write this URL into a regex template to add in as a rewrite. I've tried various regex sandboxes to figure it out on my own but they won't allow a '/' for instance when I copy an expression from here for testing:
I've got a custom post type (publications) with 2 taxonomies (magazine, issue) which I'm trying to create a good looking URL for.
After many hours I've come here to find out how I can convert this.
index.php?post_type=publications&magazine=test-mag&issue=2016-aug
To a templated regex expression (publication, magazine and issue are constant) that can output.
http://example.com/publications/test-mag/2016-aug/
Hopefully with room to extend if an article is followed through from that page.
Thanks in advance.
EDIT 1:
I've got this for my rule:
^publications/([^/]*)/([^/]*)/?$
and this for my match:
^index.php?post_type=publications&magazine=$matches[1]&issue=$matches[2]$
and testing with this:
http://localhost/publications/test-mag/2016-aug/
but its giving me a 404. What's the problem?
^index\.php\?post_type=publications&magazine=([^&]+)&issue=([^&]+)$
^ start of string
index\.php\?post_type=publications&magazine= literal text
([^&]+) one or more non-ampersand characters (will get all text up to the next url parameter. this is captured as a group
&issue= literal text
([^&]+) one or more non-ampersand characters. also captured
$ end of string
$str = 'index.php?post_type=publications&magazine=test-mag&issue=2016-aug';
preg_match('/magazine=([\w-]+?)&issue=([\w-]+)/', $str, $matches);
$res = 'http://example.com/' . $matches[1] . '/' . $matches[2] . '/';
echo $res; // => http://example.com/test-mag/2016-aug/
You can use the add_rewrite_rule method in the WP Rewrite API to accomplish this.
add_rewrite_rule('^/([^/]*)/([^/]*)/?$','index.php?post_type=publications&magazine=$matches[1]&issue=$matches[2]','top');
I am new to Regex. I have a few easy expressions written for urls. I am having some trouble writing one if the URL has a category and then a random html page after the ID:
http://www.website.com/MajorCategory/MinorCateory/118326/title-of-page.html
I am trying to match the 6 digit id in the example above. any help on getting this to match would be awesome.
You can use:
'~[0-9]+(?=/[^/]*$)~'
Working Demo: http://regex101.com/r/wY4mH3
PHP Code:
if ( preg_match('~[0-9]+(?=/[^/]*$)~', $str, $m) )
echo ($m[0]);
I need a regex pattern to match URLs in a complicated environment.
An URL would be in this position:
[url=http://www.php.net/manual/en/function.preg-replace.php:32p0eixu]TEST[/url:32p0eixu]
(That's just a sample URL)
I need to match the URL until the colon, the colon and the code after that should be ignored. There are so many URLs out there and I'm not that experienced to create a pattern to match everything from http:// to :
As I said, everything else should be ignored, left away, except the URL which I need to store in a variable.
Could someone help me create such a pattern? My tries were matching the URL above, but when I put in more complicated URLs, they wouldn't match.
This is the pattern I've created. It works with simple URLs, but not with the complicated ones:
http(s)?://[A-Za-z0-9.,/_-]+
I'm not very good in regex, I'm still learning.
Thank you.
This regex should do it for you.
\[url=(.*?):[a-zA-Z0-9]*\]
Run against your test data:
[url=http://www.php.net/manual/en/function.preg-replace.php:32p0eixu]TEST[/url:32p0eixu]
This will return the URL in capture group 1.
Assuming PHP (since your test URL is for the PHP manual), you'd use this with preg_match like this:
$value = "[url=http://www.php.net/manual/en/function.preg-replace.php:32p0eixu]TEST[/url:32p0eixu]";
$pattern = "/\[url=(.*?):[a-zA-Z0-9]*\]/";
preg_match($pattern, $value, $matches);
echo $matches[1];
Output:
http://www.php.net/manual/en/function.preg-replace.php
This will also work against URLs which contain colons in them, such as:
http://www.php.net:8080/manual/en/function.preg-replace.php
http://www.php.net/manual/us:en/function.preg-replace.php
How about this:
^(http(s)?:\/\/)?[^]^(^)^ ]+
Below regex will give you the url part before colon:
\[url=((http|https)?://)?[^\:]+
I want a regex that will always return the last part of an url before the query string parameters and without the jessionid if present.
Here's some url examples:
http://www.somesite.com/some/path/test.action;jsessionid=000063vCmvJAn7VWyymA_dPsHZs:16u9pglit?sort=2¶m1=1¶m2=2
http://www.somesite.com/some/path/test;jsessionid=000063vCmvJAn7VWyymA_dPsHZs:16u9pglit?sort=2¶m1=1¶m2=2
http://www.somesite.com/some/path/test.action?sort=2¶m1=1¶m2=2
http://www.somesite.com/some/path/test?sort=2¶m1=1¶m2=2
Here's my regex so far:
.*http://.*/some/path.*/(.*);?.*\?.*
It is working for the url that does not contain jsessionid, but will return test;jessionid=... if it is present.
To test: http://regex101.com/r/fM0mE2
I would use this regex:
.*http:\/\/.*\/some\/path.*\/([^;\?]+);?.*\?.*
^^^^^^
Basically matches anything that isn't ; or ?. And I think it might be shortened to:
.*http:\/\/.*\/some\/path.*\/([^;\?]+)