bobbinsbro Posted June 8, 2010 Share Posted June 8, 2010 hi all how would i go about using a backreference in a character class in a pcre regex pattern? example of something similar to what i want to do: $pattern = '/(abc)([^\1]).*/'; thanks in advance bob Quote Link to comment https://forums.phpfreaks.com/topic/204195-backreference-regex-help/ Share on other sites More sharing options...
cags Posted June 8, 2010 Share Posted June 8, 2010 Erm... like that. I don't understand the question. Given an example input with expected output. Quote Link to comment https://forums.phpfreaks.com/topic/204195-backreference-regex-help/#findComment-1069527 Share on other sites More sharing options...
bobbinsbro Posted June 8, 2010 Author Share Posted June 8, 2010 the problem is that doesn't work. i'm trying to get the contents of a string delimited by either double quotes ("...") or single quotes ('...'). the regex i'm using is: $pattern = '/(\'|")([^\1]*)(?:\1)/s' that doesn't work. however, changing it to $pattern = '/(\'|")([^\']*)(?:\1)/s' does work (assuming the string is delimited by single quotes). i checked and made sure that \1 contains a single or double quote (used preg_match() with the optional &$matches argument and print_r()'ed $matches to see what is in the sub-patterns). as a result, i assume either i can't use a backreference in a character class or i'm doing something wrong. Quote Link to comment https://forums.phpfreaks.com/topic/204195-backreference-regex-help/#findComment-1069542 Share on other sites More sharing options...
gizmola Posted June 8, 2010 Share Posted June 8, 2010 Taking you at your word, if I was going to construct this regex I would use something like: $pattern = '/(\'|")(.*?)(\1)/s'; Not sure why you are trying to match things that aren't the backreference when you will already use the backreference to delimit your match. Quote Link to comment https://forums.phpfreaks.com/topic/204195-backreference-regex-help/#findComment-1069571 Share on other sites More sharing options...
bobbinsbro Posted June 8, 2010 Author Share Posted June 8, 2010 the pattern i gave is part of a much longer pattern, that is supposed to match long blocks of text containing many quote-delimited strings. i originally tried what you suggested, but preg_match() didn't stop at the first quote (the one on which i need to end the sub-pattern) but rather continued to the very last quote, counting all intermediate quotes as part of the (.*) sub-pattern. so i tried using matching any character that isn't the backreference to force preg_match() to stop on the first quote found. i need to use the back reference because i need to allow both single and double quotes in all places, and i can't know which in advance. example of a string i want to match: 'the brown fox' 'jumped over the lazy dog' and the pattern $pattern = '/(\'|")([^\1])(\1)/s'; should match only the brown fox but doesn't match anything. using $pattern = '/(\'|")(.*)(\1)/s'; matches the brown fox' 'jumped over the lazy dog which is not what i want Quote Link to comment https://forums.phpfreaks.com/topic/204195-backreference-regex-help/#findComment-1069590 Share on other sites More sharing options...
gizmola Posted June 8, 2010 Share Posted June 8, 2010 You didn't look at my pattern carefully. What you're talking about is the default of greedy. To make the match non-greedy I added the '?' to the match all characters -> '.*?' Quote Link to comment https://forums.phpfreaks.com/topic/204195-backreference-regex-help/#findComment-1069613 Share on other sites More sharing options...
bobbinsbro Posted June 8, 2010 Author Share Posted June 8, 2010 thank you very much, that did the trick. i didn't know about greedy and non-greedy. nice to learn new things. Quote Link to comment https://forums.phpfreaks.com/topic/204195-backreference-regex-help/#findComment-1069640 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.