jjzjj

ruby - 匹配不包含特定单词的字符串

我正在使用ruby​​和match方法,我想用正则表达式匹配一个不包含特定字符串的URL:例如:http://website1.com/url_with_some_words.htmlhttp://website2.com/url_with_some_other_words.htmlhttp://website3.com/url_with_the_word_dog.html我想匹配不包含单词dog的URL,所以应该匹配第一个和第二个 最佳答案 只需使用否定前瞻^(?!.*dog).*$。解释^:匹配行首(?!.*dog):否定前瞻,

ruby-on-rails - Ruby 2.0.0 字符串#Match ArgumentError : invalid byte sequence in UTF-8

我经常看到这种情况,但还没有想出一个优雅的解决方案。如果用户输入包含无效的字节序列,我需要能够让它不引发异常。例如:#@raw_responsecomesfromuserandcontainsinvalidUTF-8#forexample:@raw_response="\xBF"regex.match(@raw_response)ArgumentError:invalidbytesequenceinUTF-8已经问了很多类似的问题,结果似乎是对字符串进行编码或强制编码。然而,这些都不适合我:regex.match(@raw_response.force_encoding("UTF-8"

ruby 正则表达式 : match and get position(s) of

我想匹配一个正则表达式并获取匹配字符串中的位置例如,"AustinTexasDallasTexas".match_with_posn/(Texas)/我想要match_with_posn返回类似:[6,17]其中6和17是单词Texas的两个实例的起始位置。有这样的吗? 最佳答案 使用Ruby1.8.6+,你可以这样做:require'enumerator'#Onlyfor1.8.6,newerversionsshouldnotneedthis.s="AustinTexasDallasTexas"positions=s.enum_f

ruby-on-rails - 为什么 Rspec 说 "Failure/Error: Unable to find matching line from backtrace"?

我在这里学习Rails教程:http://railstutorial.org/chapters/filling-in-the-layout#top当我运行“rspecspec/”时,我得到一堆如下所示的错误:1)LayoutLinksshouldhaveaHomepageat'/'Failure/Error:Unabletofindmatchinglinefrombacktracestackleveltoodeep#C:/Ruby19/lib/ruby/1.9.1/forwardable.rb:1852)LayoutLinksshouldhaveaContactpageat'/cont

ruby - 检查字符串是否与 ruby​​ 中的正则表达式匹配的最快方法?

检查字符串是否与Ruby中的正则表达式匹配的最快方法是什么?我的问题是我必须“egrep”一个巨大的字符串列表以找到哪些匹配运行时给定的正则表达式。我只关心字符串是否匹配正则表达式,不关心匹配到哪里,也不关心匹配组的内容是什么。我希望这个假设可以用来减少我的代码花在匹配上的时间正则表达式。我加载正则表达式pattern=Regexp.new(ptx).freeze我发现string=~pattern比string.match(pattern)稍快。是否有其他技巧或快捷方式可用于使此测试更快? 最佳答案 从Ruby2.4.0开始,您可

javascript - 如何使用正则表达式解析 OCC 期权符号?

OCC期权符号由4部分组成:标的股票或ETF的根符号,用空格填充到6个字符到期日期,6位数字,格式为yymmdd看跌或看涨期权类型,P或C行权价,为价格x1000,前面用0到8位数字填充例如,SPX141122P00019500表示SPX看跌期权,2014年11月22日到期,行使价为19.50美元。是否可以使用正则表达式自动解析出来?我正在使用JavaScript 最佳答案 这是正则表达式(我强烈推荐http://regexr.com)([\w]{6})((\d{2})(\d{2})(\d{2}))([PC])(\d{8})第1组:

javascript - Javascript 中的正则表达式没有它应该的那么贪婪?

我编写了一个简单的代码来捕获字符串中的特定组:/[a-z]+([0-9]+)[a-z]+/gi(nchars,mdigts,kchars).code:varmyString='aaa111bbb222ccc333ddd';varmyRegexp=/[a-z]+([0-9]+)[a-z]+/gi;varmatch=myRegexp.exec(myString);console.log(match)while(match!=null){match=myRegexp.exec(myString);console.log(match)}结果是:["aaa111bbb","111"]["ccc3

javascript - 在 Javascript 中提取子字符串的更简单方法

我正在尝试从Javascript中的字符串中提取与给定模式匹配的子字符串。示例:varclassProp='activecategory_games',match=classProp.match(/category_[a-z]+\b/),category;if(match!==null&&match.length>0){category=match[0];}有没有更简单的方法来实现这个目标?单线,最好? 最佳答案 category前要不要有\b?如果匹配失败,您可以通过提供一个空数组来缩短它;category=(classProp.

javascript - 使用 .match 和 .search 的区别

我为coderbyte使用了以下代码:functionVowelCount(str){//codegoesherereturnstr.match(/[aeiou]/gi).length;}//keepthisfunctioncallhere//toseehowtoenterargumentsinJavaScriptscrolldownprint(VowelCount(readline()));我理解大部分代码,除了以下部分:正斜杠和方括号的作用是什么?gi有什么作用?search()和match()有什么区别?我应该在什么情况下使用什么? 最佳答案

javascript - JavaScript 正则表达式中 'y' 粘性模式修饰符的用途是什么?

MDN为JavaScriptRegExp引入了“y”粘性标志。这是一个documentationexcerpt:ysticky;matchesonlyfromtheindexindicatedbythelastIndexpropertyofthisregularexpressioninthetargetstring(anddoesnotattempttomatchfromanylaterindexes).还有一个例子:vartext='Firstline\nSecondline';varregex=/(\S+)line\n?/y;varmatch=regex.exec(text);co