Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx How to find text between two strings

Tags:

regex

I have this text and I want to capture

begin
text 
end

begin
text 
end

begin
text 
end

the text between the begin and end.

/begin.*end/

this will capture the first begin and the last end.

like image 804
barak Avatar asked Sep 09 '11 11:09

barak


People also ask

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

How do you match line breaks in regex?

If you want to indicate a line break when you construct your RegEx, use the sequence “\r\n”. Whether or not you will have line breaks in your expression depends on what you are trying to match. Line breaks can be useful “anchors” that define where some pattern occurs in relation to the beginning or end of a line.

What does =~ mean in Ruby regex?

=~ is Ruby's basic pattern-matching operator. When one operand is a regular expression and the other is a string then the regular expression is used as a pattern to match against the string. (This operator is equivalently defined by Regexp and String so the order of String and Regexp do not matter.


1 Answers

Make it lazy - /begin.*?end/

Sidenote: "lazy" is no less acceptable than "non-greedy" is. Example, example, example

like image 81
Leonid Avatar answered Sep 25 '22 07:09

Leonid