首页 \ 问答 \ 有没有比使用正则表达式更快地解析主题标签的方法?(Is there a faster way to parse hashtags than using Regular Expressions?)

有没有比使用正则表达式更快地解析主题标签的方法?(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 string This 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
更新时间:2022-01-29 09:01

最满意答案

那么,首先考虑哪些数据位自然地结合在一起。 例如,如果你有一名球员的名字,得分,位置,生命数量等等,这就表明你应该有一个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.

相关问答

更多
  • 使用基本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#类,该项目实现了一个ref类子类,这个子类在/ clr编译源代码中定义,如下所示: public ref class CPPRefClass:public CSharpClass 这当然意味着CPPRefClass仍然是一个.NET类,但是您可以在使用委托的纯C ++类中使用它。 您甚至可以使用gcroot模板将其定义为C ++本机类的成员: class NativeClass { private: gcroot m_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, ...
  • 听起来你正在做很多体操,因为你有一个特定的形式。 我建议首先尝试使数据整洁 。 在不阅读链接的情况下,快速摘要是将您的数据放入单个数据框中,以便轻松处理。 答案的快速版本(这里我使用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) { %> <% }); %> ...

相关文章

更多

最新问答

更多
  • python的访问器方法有哪些
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。
  • 响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)
  • 在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)
  • NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)
  • 元素上的盒子阴影行为(box-shadow behaviour on elements)
  • Laravel检查是否存在记录(Laravel Checking If a Record Exists)
  • 设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
  • 想学Linux 运维 深圳有哪个培训机构好一点
  • 为什么有时不需要在lambda中捕获一个常量变量?(Why is a const variable sometimes not required to be captured in a lambda?)
  • 在Framework 3.5中使用服务器标签<%=%>设置Visible属性(Set Visible property with server tag <%= %> in Framework 3.5)
  • AdoNetAppender中的log4net连接类型无效(log4net connection type invalid in AdoNetAppender)
  • 错误:发送后无法设置标题。(Error: Can't set headers after they are sent. authentication system)
  • 等待EC2实例重启(Wait for an EC2 instance to reboot)
  • 如何在红宝石中使用正则表达式?(How to do this in regex in ruby?)
  • 使用鼠标在OpenGL GLUT中绘制多边形(Draw a polygon in OpenGL GLUT with mouse)
  • 江民杀毒软件的KSysnon.sys模块是什么东西?
  • 处理器在传递到add_xpath()或add_value()时调用了什么顺序?(What order are processors called when passed into add_xpath() or add_value()?)
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • SQL查询,其中字段不包含$ x(SQL Query Where Field DOES NOT Contain $x)
  • PerSession与PerCall(PerSession vs. PerCall)
  • C#:有两个构造函数的对象:如何限制哪些属性设置在一起?(C#: Object having two constructors: how to limit which properties are set together?)
  • 平衡一个精灵(Balancing a sprite)
  • n2cms Asp.net在“文件”菜单上给出错误(文件管理器)(n2cms Asp.net give error on Files menu (File Manager))
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的