有没有比使用正则表达式更快地解析主题标签的方法?(Is there a faster way to parse hashtags than using Regular Expressions?)
我很好奇,除了使用正则表达式(主要是Ruby)之外,有没有更快/更好的方法来解析字符串中的hashtags?
编辑
例如,我想解析字符串This is a #hashtag, and this is #another one!
并获得#another
和#hashtag
#another
。 我正在使用#\S+
作为我的正则表达式。I am curious, is there a faster/better way to parse hashtags in a string, other than using Regular Expressions (mainly in Ruby)?
Edit
For example I want to parse the stringThis is a #hashtag, and this is #another one!
and get the words#hashtag
and#another
. I am using#\S+
for my regex.
原文:https://stackoverflow.com/questions/13904934
最满意答案
那么,首先考虑哪些数据位自然地结合在一起。 例如,如果你有一名球员的名字,得分,位置,生命数量等等,这就表明你应该有一个
Player
类。 因此,请查看所有状态,将其分为不同的组,然后检查您现有的哪些方法仅适用于一组状态或另一组状态......并将方法置于其感兴趣的状态。当一个方法需要处理来自多个对象的状态时,它会变得更复杂:当然,这通常意味着您将代码放在一个类型中,并将另一个类型作为参数传递。
很多这是直觉,说实话 - 以上只是起点。 随着时间的推移,它会变得更容易一些,并且您可能会开始更多地考虑责任而不仅仅是数据 - 但这是一个很好的起点,因为您可能已经拥有了所需的数据,而没有以正确的方式进行组织。而决定系统内的责任可能会更加困难。
Well, start off by thinking of which bits of data go together naturally. For example, if you have a player's name, score, position, number of lives etc, that would suggest you should have a
Player
class. So take a look at all the state, split it up into different groups, and then check which of your existing methods only work with one group of state or another... and put the methods with the state they're interested in.It gets trickier when one method needs to deal with state from multiple objects, of course: typically that means you'll put the code in one type and pass the other type in as a parameter.
A lot of this is gut feeling, to be honest - the above are only starting points. Over time it gets somewhat easier, and you'll probably start to think more in terms of responsibilities than just data - but it's a good starting point, as you probably already have the data you need, just not organized in the right way... whereas deciding on the responsibilities within the system can be a lot harder.
相关问答
更多-
需要重新组织数据(need to reorganize data)[2022-11-12]
使用基本R的解决方案( reshape2::melt除外): dat2 <- reshape2::melt(dat, id.vars=c("Poly","Tran","Strat","Surv")) dat2 <- subset(dat2, subset = value > 0) dat2$variable <- as.character(dat2$variable) dat2$Status <- with(dat2, substr(tolower(variable), nchar(variable), n ... -
每个Eloquent结果都是一个包含大量有用方法的集合。 在这种情况下,您可以将sortBy与回调函数一起使用: $articles = Article::orderBy('id', 'desc') ->with(['user', 'category']) // add with to optimize your query relations ->take(100)->get(); $sorted = $article ...
-
在c ++中扩展C#类(Extending C# classes in c++)[2022-03-17]
你可以在一个C ++项目中扩展一个C#类,该项目实现了一个ref类子类,这个子类在/ clr编译源代码中定义,如下所示: public ref class CPPRefClass:public CSharpClass 这当然意味着CPPRefClass仍然是一个.NET类,但是您可以在使用委托的纯C ++类中使用它。 您甚至可以使用gcroot模板将其定义为C ++本机类的成员: class NativeClass { private: gcrootm_CPPRefCla ... -
首先,您需要实现可以帮助您拖放控件的功能。但在此之前,您需要将控件的当前位置存储在某些类级变量中。请考虑以下代码片段: int xloc; int yloc; ///other codes in-between private void btn1_Click() { xloc = this.btn1.Location.X; yloc = this.btn1.Location.Y; } 现在拖放功能: private Point setNewLocation; privat ...
-
如果你只需要处理这个特定的情况,即转换为 (((Int, String), Maybe Int), (Char, Int)) 至 (Int, (Char, (Maybe Int, (String, (Int, ())))) 那么,根据是否要保留Int Component的顺序或交换它们,可以简单地使用以下两个函数之一: from1 (((m, s), mb), (c, n)) = (m, (c, mb, (s, (n, ())))) from2 (((m, s), mb), (c, n)) = (n, ...
-
在R中,根据元素名称(rbind和指示符变量)重新组织列表(In R, reorganize list based on element names (rbind and indicator variable))[2023-09-09]
听起来你正在做很多体操,因为你有一个特定的形式。 我建议首先尝试使数据整洁 。 在不阅读链接的情况下,快速摘要是将您的数据放入单个数据框中,以便轻松处理。 答案的快速版本(这里我使用lst而不是list作为名称以避免与内置list混淆)是这样做的: do.call(rbind, lapply(seq(lst), function(i) { lst[[i]]$type <- names(lst)[i]; lst[[i]] }) ) 这将做的是创建一个数据框,其中包含“type”列,其中包含该 ... -
那么,首先考虑哪些数据位自然地结合在一起。 例如,如果你有一名球员的名字,得分,位置,生命数量等等,这就表明你应该有一个Player类。 因此,请查看所有状态,将其分为不同的组,然后检查您现有的哪些方法仅适用于一组状态或另一组状态......并将方法置于其感兴趣的状态。 当一个方法需要处理来自多个对象的状态时,它会变得更复杂:当然,这通常意味着您将代码放在一个类型中,并将另一个类型作为参数传递。 很多这是直觉,说实话 - 以上只是起点。 随着时间的推移,它会变得更容易一些,并且您可能会开始更多地考虑责任而不 ...
-
你真的回答了它: as.integer(factor(sort(z))) 我知道这已经被接受了,但我决定看看factor()来看看它是如何完成的。 它或多或少归结于此: x <- sort(z) match(x, unique(x)) 这是一个额外的路线我想,但它应该更快,如果这件事。 You answered it yourself really: as.integer(factor(sort(z))) I know this has been accepted already but I deci ...
-
我们可以使用pd.melt方法: In [87]: pd.melt(df, 'Type') Out[87]: Type variable value 0 A Label_1 1 1 B Label_1 3 2 C Label_1 2 3 A Label_2 5 4 B Label_2 2 5 C Label_2 1 6 A Label_3 3 7 B Label_3 ...
-
您需要使用glob语法修改grunt文件以查找所有子目录中的javascript文件(这在添加新模块时尤其重要)。 ngbp项目有一个使用这种格式的更复杂的Gruntfile.js的很好的例子。 Gruntfile收集项目中的所有脚本,然后在构建index.html时将它们包含在循环中,如下所示: <% scripts.forEach(function(file) { %> <% }); %> ...