Ruby中具有前瞻性的正则表达式 [英] Regular Expressions with lookahead in Ruby

查看:93
本文介绍了Ruby中具有前瞻性的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的正则表达式之争是替换字符串中数字前的所有逗号.然后,正则表达式必须忽略所有以下逗号.我已经在rubular上纠缠了大约一个小时,似乎还无法正常工作.

My current regex battle is replacing all commas before a number in a string. The regex must then ignore all following commas. I've been screwing around on rubular for about an hour and can't quite seem to get something working.

测试字符串...

'this is, a , sentence33 Here, is another.'

所需的输出...

'this is comma a comma sentence33 Here, is another.'

所以……

testString.gsub(/\,*\d\d/,"comma")

为了给您提供一些背景知识,我正在做一些附带的项目.我正在收集的元素在很大程度上以逗号分隔,从两位数的年龄开始.但是,有时在标题之前可能包含逗号.为了保留以后要设置的结构,我需要替换标题中的逗号.

To give you some background, I'm doing a little scraping sideproject. The elements I'm gathering are largely comma separated beginning with a two digit age. However sometimes theres a headline preceeding the age that may contain commas. To preserve the structure I set up later on, I need to replace the commas in the headline.

尝试堆栈溢出后的答案...

AFTER TRYING STACK OVERFLOW'S ANSWER...

我仍然遇到一些问题.别笑了,但这是屏幕抓取的实际内容,这会引起问题...

I'm still having some issues. Don't laugh but here's the actual line from the screen scraping thats causing problems...

statsString =     "              23,  5'9\",  140lb,  29w,                        Slim,                 Brown       Hair,             Shaved Body,              White,    Looking for       Friendship,    1-on-1 Sex,    Relationship.   Out      Yes,SmokeNo,DrinkNo,DrugsNo,ZodiacCancer.      Versatile,                  7.5\"                    Cut, Safe Sex Only,     HIV      Negative, Prefer meeting at:Public Place.                   PerformerContact  xxxxxx87                                                   This user has TURNED OFF his IM                                     Send Smile      Write xxxxxx87 a message:" 

首先在所有这些片段中添加"xx",以便逗号过滤在所有情况下都适用,无论年龄有无文本都适用.其次是实际修复.输出如下...

First to all of these fragments I add 'xx, ' so that my comma filtering will work in all cases, those with and without text ahead of the age. Followed by the actual fix. The output is below...

statsString = 'xx, ' + statsString

statsString = statsString.gsub(/\,(?=.*\d)/, 'comma');

 => "xxcomma               23comma  5'9\"comma  140lbcomma  29wcomma                        Slimcomma                 Brown       Haircomma             Shaved Bodycomma              Whitecomma    Looking for       Friendshipcomma    1-on-1 Sexcomma    Relationship.   Out      YescommaSmokeNocommaDrinkNocommaDrugsNocommaZodiacCancer.      Versatilecomma                  7.5\"                    Cutcomma Safe Sex Onlycomma     HIV      Negativecomma Prefer meeting at:Public Place.                   PerformerContact  xxxxx87                                                   This user has TURNED OFF his IM                                     Send Smile      Write xxxxxxx87 a message:" 

推荐答案

代码:

Code:

testString = 'this is, a , sentence33 Here, is another.';
result = testString.gsub(/\,(?=.*\d)/, 'comma');
print result;

输出:

Output:

this iscomma a comma sentence33 Here, is another.

测试:

Test:

http://ideone.com/9nt1b

这篇关于Ruby中具有前瞻性的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆