Suppose I have the following line: "BENffew123X\r\nBENx432f456X\r\nBEN!233789X\r\nBEN4545789X" I want a regular expression that will catch "BEN! 233789", it should look non-greedily for "BEN" followed by any character (excluding the word "BEN") and ending with 789X. I tried the regex: /BEN.+?789X/mi and I get "BENffew123X\r\nBENx432f456X\r\nBEN!233789X" as a match. I understand that this regular expression searches for the first βBENβ and catches the beginning of the line, but I want it to look for βBENβ, which is closest to the first β789Xβ. How can i achieve this? One idea is to flip a line, should I do this?
ruby regex
benams
source share