Daedalian Posted July 31, 2008 Share Posted July 31, 2008 Hello! I've got a small problem with regex. I have a text with some code listings, with structure like this: Bla bla bla lorem ipsum dolore sit amet. Bla bla bla lorem ipsum dolore sit amet. <code> lots of multilined code</code> Bla bla bla. <code> lots of another multilined code</code> I want to match both <code>'s. I do this way: /\<code\>.*\<\/code\>/s. It works, but it matches: <code> lots of multilined code</code> Bla bla bla. <code> lots of another multilined code</code> While I want it to match: <code> lots of multilined code</code> and <code> lots of another multilined code</code> Possibly this is somehow related to greedyness? I tried everything though, no success Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/117531-solved-two-tags-gotta-match-them-separately/ Share on other sites More sharing options...
rhodesa Posted July 31, 2008 Share Posted July 31, 2008 to stop the greedness, add a ? after the *...like so: /\<code\>.*?\<\/code\>/s Link to comment https://forums.phpfreaks.com/topic/117531-solved-two-tags-gotta-match-them-separately/#findComment-604525 Share on other sites More sharing options...
Daedalian Posted July 31, 2008 Author Share Posted July 31, 2008 That was the fastest reply on forums I saw in my life. It worked. Thank you! Link to comment https://forums.phpfreaks.com/topic/117531-solved-two-tags-gotta-match-them-separately/#findComment-604528 Share on other sites More sharing options...
rhodesa Posted July 31, 2008 Share Posted July 31, 2008 it's nice to know that being quick is sometimes a good thing Link to comment https://forums.phpfreaks.com/topic/117531-solved-two-tags-gotta-match-them-separately/#findComment-604531 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.