Regular Expressions with lookahead in Ruby

拟墨画扇 提交于 2019-12-23 01:28:11

问题


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.

Test String...

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

Desired Output...

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

So something along the lines of...

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:" 

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:" 

回答1:


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




回答2:


Not so short, but, seems to solve your task:

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

str = str.match(/(.*)(\d+.*)/) do

    before = $1
    tail = $2

    before.gsub( /,/, 'comma' ) + tail
end

print str


来源:https://stackoverflow.com/questions/10237474/regular-expressions-with-lookahead-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!