Prev: form validation code
Next: Is this a bug with date() ?
From: Gaurav Kumar on 30 Jun 2010 10:12 Hi All, Need help in resolving the below problem- I would like to get the whole comma separated string into an array value- 1. $postText = "chapters 5, 6, 7, 8"; OR 2. $postText = "chapters 5, 6; OR 3. $postText = "chapters 5, 6, 7"; What i have done so far is- preg_match('/chapter[s]*[ ]*(\d+, \d+, \d+)/i', $postText, $matches); The above will exactly match the third value $postText = "chapters 5, 6, 7"; By Above $matches will contain a value of : $matches[1] = '5, 6, 7'; Now i need a SINGLE regular expression which can match first, second variable above or any number of comma separated string and IMPORTANTLY provide me that whole comma separated sting value (as above) in single array key element like below- $matches[1] = '5, 6, 7'; OR $matches[1] = '5, 6'; OR $matches[1] = '5, 6, 7, 8, 9'; Also I have to use regular expression only as the flow of the code does not permit to use any other method to get comma separated string from the master/base string. Thanks, Gaurav Kumar
From: "Jo�o C�ndido de Souza Neto" on 30 Jun 2010 10:23 Not tested, but I think it should work: preg_match_all('/(\d+),/', $postText, $matches); -- Jo�o C�ndido de Souza Neto "Gaurav Kumar" <kumargauravjukebox(a)gmail.com> escreveu na mensagem news:AANLkTikDb_ISmNkPoMicXzsfZIxg4deDzNuNrcimjud6(a)mail.gmail.com... > Hi All, > > Need help in resolving the below problem- > > > I would like to get the whole comma separated string into an array value- > > 1. $postText = "chapters 5, 6, 7, 8"; > OR > 2. $postText = "chapters 5, 6; > OR > 3. $postText = "chapters 5, 6, 7"; > > What i have done so far is- > preg_match('/chapter[s]*[ ]*(\d+, \d+, \d+)/i', $postText, $matches); > > The above will exactly match the third value $postText = "chapters 5, 6, > 7"; > > By Above $matches will contain a value of : $matches[1] = '5, 6, 7'; > > Now i need a SINGLE regular expression which can match first, second > variable above or any number of comma separated string and IMPORTANTLY > provide me that whole comma separated sting value (as above) in single > array > key element like below- > > $matches[1] = '5, 6, 7'; > OR > $matches[1] = '5, 6'; > OR > $matches[1] = '5, 6, 7, 8, 9'; > > > Also I have to use regular expression only as the flow of the code does > not > permit to use any other method to get comma separated string from the > master/base string. > > Thanks, > > Gaurav Kumar >
From: Richard Quadling on 30 Jun 2010 10:22 On 30 June 2010 15:12, Gaurav Kumar <kumargauravjukebox(a)gmail.com> wrote: > Hi All, > > Need help in resolving the below problem- > > > I would like to get the whole comma separated string into an array value- > > 1. $postText = "chapters 5, 6, 7, 8"; > OR > 2. $postText = "chapters 5, 6; > OR > 3. $postText = "chapters 5, 6, 7"; > > What i have done so far is- > preg_match('/chapter[s]*[ ]*(\d+, \d+, \d+)/i', $postText, $matches); > > The above will exactly match the third value $postText = "chapters 5, 6, 7"; > > By Above $matches will contain a value of : $matches[1] = '5, 6, 7'; > > Now i need a SINGLE regular expression which can match first, second > variable above or any number of comma separated string and IMPORTANTLY > provide me that whole comma separated sting value (as above) in single array > key element like below- > > $matches[1] = '5, 6, 7'; > OR > $matches[1] = '5, 6'; > OR > $matches[1] = '5, 6, 7, 8, 9'; > > > Also I have to use regular expression only as the flow of the code does not > permit to use any other method to get comma separated string from the > master/base string. > > Thanks, > > Gaurav Kumar > Try ... preg_match('/chapters? ?((?:\d++(?:, )?)+)/im', $old_string, $a_Matches)) chapters? ?((?:\d++(?:, )?)+) Match the characters âchapterâ literally «chapter» Match the character âsâ literally «s?» Between zero and one times, as many times as possible, giving back as needed (greedy) «?» Match the character â â literally « ?» Between zero and one times, as many times as possible, giving back as needed (greedy) «?» Match the regular expression below and capture its match into backreference number 1 «((?:\d++(?:, )?)+)» Match the regular expression below «(?:\d++(?:, )?)+» Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» Match a single digit 0..9 «\d++» Between one and unlimited times, as many times as possible, without giving back (possessive) «++» Match the regular expression below «(?:, )?» Between zero and one times, as many times as possible, giving back as needed (greedy) «?» Match the characters â, â literally «, » Created with RegexBuddy -- ----- Richard Quadling "Standing on the shoulders of some very clever giants!" EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling
From: Gaurav Kumar on 1 Jul 2010 01:47 Hey Richard, Thanks!!! You have resolved my problem.. GK On Wed, Jun 30, 2010 at 7:42 PM, Gaurav Kumar <kumargauravjukebox(a)gmail.com>wrote: > Hi All, > > Need help in resolving the below problem- > > > I would like to get the whole comma separated string into an array value- > > 1. $postText = "chapters 5, 6, 7, 8"; > OR > 2. $postText = "chapters 5, 6; > OR > 3. $postText = "chapters 5, 6, 7"; > > What i have done so far is- > preg_match('/chapter[s]*[ ]*(\d+, \d+, \d+)/i', $postText, $matches); > > The above will exactly match the third value $postText = "chapters 5, 6, > 7"; > > By Above $matches will contain a value of : $matches[1] = '5, 6, 7'; > > Now i need a SINGLE regular expression which can match first, second > variable above or any number of comma separated string and IMPORTANTLY > provide me that whole comma separated sting value (as above) in single array > key element like below- > > $matches[1] = '5, 6, 7'; > OR > $matches[1] = '5, 6'; > OR > $matches[1] = '5, 6, 7, 8, 9'; > > > Also I have to use regular expression only as the flow of the code does not > permit to use any other method to get comma separated string from the > master/base string. > > Thanks, > > Gaurav Kumar > > >
From: Richard Quadling on 1 Jul 2010 04:29 On 1 July 2010 06:47, Gaurav Kumar <kumargauravjukebox(a)gmail.com> wrote: > Hey Richard, > > Thanks!!! You have resolved my problem.. > > GK > > > On Wed, Jun 30, 2010 at 7:42 PM, Gaurav Kumar > <kumargauravjukebox(a)gmail.com>wrote: > >> Hi All, >> >> Need help in resolving the below problem- >> >> >> I would like to get the whole comma separated string into an array value- >> >> 1. $postText = "chapters 5, 6, 7, 8"; >> OR >> 2. $postText = "chapters 5, 6; >> OR >> 3. $postText = "chapters 5, 6, 7"; >> >> What i have done so far is- >> preg_match('/chapter[s]*[ ]*(\d+, \d+, \d+)/i', $postText, $matches); >> >> The above will exactly match the third value $postText = "chapters 5, 6, >> 7"; >> >> By Above $matches will contain a value of : $matches[1] = '5, 6, 7'; >> >> Now i need a SINGLE regular expression which can match first, second >> variable above or any number of comma separated string and IMPORTANTLY >> provide me that whole comma separated sting value (as above) in single array >> key element like below- >> >> $matches[1] = '5, 6, 7'; >> OR >> $matches[1] = '5, 6'; >> OR >> $matches[1] = '5, 6, 7, 8, 9'; >> >> >> Also I have to use regular expression only as the flow of the code does not >> permit to use any other method to get comma separated string from the >> master/base string. >> >> Thanks, >> >> Gaurav Kumar >> >> >> > No problem. If you are on Windows, then the RegexBuddy application from JGSoft is pretty good. It allows you to build regex in a step by step way with a full description of what the regex will do, along with testing and code snippets. Also has a lot of examples to work from and a built in support forum. BTW. I'm not connected to JGSoft. Just a happy licensee. Richard. -- ----- Richard Quadling "Standing on the shoulders of some very clever giants!" EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling
|
Pages: 1 Prev: form validation code Next: Is this a bug with date() ? |