简体   繁体   中英

Issues with using regex in 'gsub'

I am puzzled why this is not working as expected.

d = "936589_WI_RAPIDS_STREET_DEPT"
f=d.gsub('/936*[0-9]_/',' ')
# => "936589_WI_RAPIDS_STREET_DEPT" 

I tested my regex by itself and it gives me the desired output:

"WI_RAPIDS_STREET_DEPT"

gsub accept a regex object, not a string .

Try:

f=d.gsub(/936*[0-9]_/, ' ')

And your regex should probably be as below:

f=d.gsub(/936[0-9]*_/, ' ')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM