简体   繁体   中英

Python to ruby regex search conversion

I have a simple python regex re.search('<span[\\w\\W]*?>Members[\\w\\W]*?([\\w\\W]*?)</span>', str) . I'd like to do the same in ruby.

From the docs, it appears that match should work. However when I try

/<span[\\w\\W]*?>Members[\\w\\W]*?([\\w\\W]*?)</span>/.match(str) I get a syntax error.

I know this is something obvious but would love some help. Thank you

You need to escape the / which is inside closing tag of </span>

/<span[\w\W]*?>Members[\w\W]*?([\w\W]*?)<\/span>/.match(str)
//                                       ^^

otherwise this will be considered as end of regex

and you can use .*? where . mean capture anything except line break

/<span.*?>Members.*?(.*?)<\/span>/.match(str)

如果要匹配的斜杠很多,请尝试使用%r表示法:

%r{<span[\w\W]*?>Members[\w\W]*?([\w\W]*?)</span>}.match(str)

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