Isn't the autonomy of all-numitter twitter hashtags in perl?

I am making HTML from twitter search results. Luckily using the Net :: Twitter module :-)

One of the rules on Twitter is that all-numeric hashtags are not links. This makes it possible to unambiguously tweet things like "ur not my # 1" like here: http://twitter.com/natarias2007/status/11246320622

The solution I came up with looks like this:

$tweet =~ s{#([0-9]*[A-Za-z_]+[0-9]*)}{<a href="http://twitter.com/search?q=%23$1">#$1</a>}g;

      

Seems to work (let's hope), but I'm still wondering ... how would you go about it?

EDIT: This regex I chose earlier was wrong! see below for a better answer :-)

+2


a source to share


2 answers


Your regex will not capture anchors containing more than one letter separated by numbers, for example. # A0A:

my @anchors = ($tweet =~ m/#(\w+)/g);
foreach my $anchor (@anchors)
{
    next unless $anchor =~ m/[a-z]/i;
    $tweet =~ s{#$anchor}{<a href="http://twitter.com/search?q=%23$anchor">#$anchor</a>}g;
}

      

eg. considermy $tweet = "hello #123 hello #abc1a hello #a0a";



In your code hello #123 hello <a href="http://twitter.com/search?q=%23abc1">#abc1</a>a hello <a href="http://twitter.com/search?q=%23a9">#a0</a>a

and mine produces hello #123 hello <a href="http://twitter.com/search?q=%23abc1a">#abc1a</a> hello <a href="http://twitter.com/search?q=%23a9a">#a0a</a>

+1


a source


I didn't realize how complicated twitter text is! http://engineering.twitter.com/2010/02/introducing-open-source-twitter-text.html

I found these related hashtag strings in the Ruby library that were linked in this blog post. Don't know much Ruby - maybe more ...

# Latin accented characters (subtracted 0xD7 from the range, it a confusable multiplication sign. Looks like "x")
LATIN_ACCENTS = [(0xc0..0xd6).to_a, (0xd8..0xf6).to_a, (0xf8..0xff).to_a].flatten.pack('U*').freeze
REGEXEN[:latin_accents] = /[#{LATIN_ACCENTS}]+/o

# Characters considered valid in a hashtag but not at the beginning, where only a-z and 0-9 are valid.
HASHTAG_CHARACTERS = /[a-z0-9_#{LATIN_ACCENTS}]/io
REGEXEN[:auto_link_hashtags] = /(^|[^0-9A-Z&\/]+)(#|īŧƒ)([0-9A-Z_]*[A-Z_]+#{HASHTAG_CHARACTERS}*)/io

      

I see no reason to handle `LATIN_ACCENTS 'separately. If configured correctly, the \ w shortcut should catch all these accented characters. Maybe this is different from Ruby ... Maybe they had other reasons ...



I am currently settling for something similar to this

$tweet =~ s{#([0-9A-Z_]*[A-Z_]+\w+)}{<a href="http://twitter.com/search?q=%23$1">#$1</a>}gi

      

I can't say that he has decided yet ...

0


a source







All Articles