2
$\begingroup$

For a twitter analysis I collected many tweets. For example:

tweet = "Designing interventions for food security and prevention of \
malnutrition in Bangladesh #sensemaker http://t.co/wulTxX3vVK"

In several of these tweets there is a textstring "http://t.co/wulTxX3vVK". What they all have in commen is the first part "http://t.co/". When I use the next code:

Flatten[StringCases[ToLowerCase[tweet], RegularExpression["\\w(?<!\\d)[\\w'-]*"]]]

The output is

{"designing", "interventions", "for", "food", "security", "and", "prevention", "of", "malnutrition", "in", "bangladesh", "sensemaker", "http", "t", "co", "wultxx3vvk"}

What is the best way to retain (or delete) al parts that start with "http://t.co/-------"?

$\endgroup$
2
$\begingroup$

Just as a complement to @MikeHoneychurch, a pure RegularExpression solution:

    tweet2 = "Designing interventions for food security and prevention of
malnutrition in Bangladesh #sensemaker http://t.co/wulTxX3vVK and some other stuff"

StringReplace[tweet2, RegularExpression["http://t.co/.+?\\b"] :> ""]

or, to remove additional spaces:

StringReplace[tweet2, RegularExpression["http://t.co/.+?\\b *"] :> ""]
$\endgroup$
2
$\begingroup$

If the link is always at the end you could do something like:

StringReplace[tweet, "http://t.co/" ~~ __ ~~ EndOfString :> ""]

if not then try

tweet2 = "Designing interventions for food security and prevention of
malnutrition in Bangladesh #sensemaker http://t.co/wulTxX3vVK and some other stuff"

StringReplace[tweet2,Shortest["http://t.co/" ~~ __ ~~ WhitespaceCharacter] :> ""]
$\endgroup$
  • $\begingroup$ Thanks, Your first comment works fine. The second suggestion gives the following output: "Designing interventions for food security and prevention of malnutrition in Bangladesh #sensemaker stuff". What happend with the string 'and some other'? $\endgroup$ – Michiel van Mens Oct 1 '14 at 10:48
  • $\begingroup$ See edit which address the problem you saw. There will be times in your pattern matching that you need to match the shortest pattern or longest pattern. $\endgroup$ – Mike Honeychurch Oct 1 '14 at 12:16
  • $\begingroup$ Hi Mike,Thanks! $\endgroup$ – Michiel van Mens Oct 1 '14 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.