0

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

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

3 Comments

thanks. that works! is there a ruby equivalent of search? Don't see one for ruby and match doesn't work the same
you can use scan, and glad that I could help!
and you can use .*? instead of [\w\W]*? . mean capture anything
1

If you have a lot of slashes to match, try %r notation:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.