Find all hash tags within a text
I need to find all hash tags in this text using ActionScript 3 (AS3):
Lorem ipsum dolor #sit_amet, consetetur sadipscing, sed #diam-nonumy eirmod tempor #invidunt ut labore #et
What's a good method to开发者_如何学Python do this? Thanks Frank
// your data:
var data:String = "Lorem ipsum dolor #sit_amet, consetetur sadipscing, sed #diam-nonumy eirmod tempor #invidunt ut labore #et";
// this regular expression will match most "normal" characters, add more as needed
// it will stop at the first whitespace or linebreak it hits
var regex:RegExp = /(\#[a-zA-Z0-9_%-]*)/g;
// this object will be used to store our results
var result:Object;
// now, we run the regex until it returns null,
// that means there's nothing more that matches
while (result = regex.exec(data)) {
// since we're not using groups or anything, all we want is the 0 in the object
trace(result[0]);
}
This will trace:
#sit_amet
#diam-nonumy
#invidunt
#et
Elaborate more on "find" because I'm not sure what you want to do with them.
That said, this may meet your needs:
var str:String = "Lorem ipsum dolor #sit_amet, consetetur sadipscing, sed #diam-nonumy eirmod tempor #invidunt ut labore #et";
var array:Array = str.split("#");
Your array will contain the following - which you can prepend with a hash again to get a hashtag:
0: Lorem ipsum dolor
1: sit_amet, consetetur sadipscing, sed
2: diam-nonumy eirmod tempor
3: invidunt ut labore
4: et
If this isn't quite what you were after then you may need to look into regular expressions which are way beyond my scope of knowledge.
RE: Is there also a smart way to delete the found tags from the text afterwards?
do you just want to delete the actual hash-tag or the hash-tag and its tagged word?
this will remove only the hash-tag from the string:
myString.replace(new RegExp("#", "g"), ""));
while this will remove the hash-tag and its tagged word from the string:
myString.replace(new RegExp("#\\w*", "g"), "")
a complete tutorial and reference for regular expressions can be found here: http://www.regular-expressions.info/
--
edit:
it's a bit complicated since you want to replace parts of the string between hash tags while the entire string is essentially between hash tags throughout. therefore, using a simple regular expression lookaround will not be foolproof without writing your own string parser.
but something like this could work:
var stringData:String = "#Lorem # et #ipsum dolor #sit_amet, consetetur sadipscing, sed #diam et #word# #_hello_# #-word# #word# #1a# #2WORD2# #2012# #word-3# #word_3# #word 3#";
trace(stringData.replace(new RegExp("#(.*?)#", "g"), ""));
精彩评论