0
name="(this is 
a message
)

(other message)";

res=name.scan(/(\(.*\))/m);
puts res.join("###");

In the above code,I want to extract the matched parenthesis with the text inside the it as a whole expresstion.

I mean I want to get the following result through my code:

(this is 
a message
)###(other message)

That's to say,the length of the res should be 2 not 1, But in my code,I always get:

(this is 
a message
)
(other message)

There must be something wrong with my pattern,can any one help me to fix it?

1 Answer 1

2

You want to use a non-greedy match, so change the regexp line to:

res=name.scan(/(\(.*?\))/m);
Sign up to request clarification or add additional context in comments.

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.