Re: preg_match fails to resolve variable as a subject

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 3 December 2010 11:13, Da Rock <php-list@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
> On 12/03/10 16:33, Tamara Temple wrote:
>>
>> On Dec 2, 2010, at 11:33 PM, Da Rock wrote:
>>
>>> On 11/29/10 09:10, Richard Quadling wrote:
>>>>
>>>> On 27 November 2010 04:45, Da Rock<php-list@xxxxxxxxxxxxxxxxxxxxxxxxx>
>>>> Âwrote:
>>>>
>>>>> On 11/27/10 13:51, Tamara Temple wrote:
>>>>>
>>>>>> On Nov 26, 2010, at 7:28 PM, Da Rock wrote:
>>>>>>
>>>>>>
>>>>>>> On 11/27/10 00:57, Richard Quadling wrote:
>>>>>>>
>>>>>>>> On 26 November 2010 00:07, Da
>>>>>>>> Rock<php-list@xxxxxxxxxxxxxxxxxxxxxxxxx>
>>>>>>>> Âwrote:
>>>>>>>>
>>>>>>>>
>>>>>>>>> preg_match("/(\d{1,3})(\.)$/", exec($mixer . ' ' .
>>>>>>>>> $command),&$matches)
>>>>>>>>>
>>>>>>>>>
>>>>>>>> Can you ...
>>>>>>>>
>>>>>>>> var_dump(exec($mixer . ' ' . $command));
>>>>>>>>
>>>>>>>> I wonder if the output includes a new line which you are not
>>>>>>>> accounting for in the regex.
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>> Haven't tried yet, but isn't that what $ is for? End of line?
>>>>>>>
>>>>>>>
>>>>>>
>>>>>> $ matches end of line, but since your last match expression is to
>>>>>> match
>>>>>> explicitly the character '.' before end of line, and there's a
>>>>>> newline, it
>>>>>> won't match. Again, use trim() to get rid of the newline character at
>>>>>> the
>>>>>> end of the string returned by exec(). Looking at the output or
>>>>>>
>>>>>>
>>>>>>> And exec only gives one line- the last line of the output of the
>>>>>>> command.
>>>>>>> You have to provide a reference to an array for the entire output.
>>>>>>>
>>>>>>> var_dump gives:
>>>>>>> string(41) "Mixer vol is currently set to 75:75"
>>>>>>>
>>>>>>>
>>>>>> It also looks like \d{1,3}\. won't match anything in the output string
>>>>>> given by the command.
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>> Thank you for that- that did it. I removed the \.
>>>>>
>>>>> I am a little confused though; that regex was tested on several test
>>>>> sites
>>>>> and was deemed accurate on them, and one of them actually supplied the
>>>>> preg_match code. And if I run the command on a shell it actually
>>>>> outputs
>>>>> exactly the right subject for the regex- not to mention it got printed
>>>>> inadvertently (using system() ) via the php exactly the same. So I
>>>>> don't
>>>>> think I missed it somehow.
>>>>>
>>>>> What I have constantly seen come up (shell and php/html) is: Mixer vol
>>>>> is
>>>>> currently set to 75:75. (including the period)
>>>>>
>>>>> Don't get me wrong- you guys make sense; my system doesn't. I need to
>>>>> get to
>>>>> the bottom of this so I can ensure my code will port well. I don't want
>>>>> to
>>>>> get it all working and find it breaks when I run it on another system,
>>>>> albeit same OS (FreeBSD). Could be updates or variations of releases.
>>>>>
>>>>> Just to check again, I ran the command on the shell again and sure
>>>>> enough
>>>>> the period is no longer there- but I can't for the life of me figure
>>>>> out
>>>>> when that changed as this code has never worked (and yes, I added in
>>>>> the \.
>>>>> to match the end of line because it originally wasn't working). Another
>>>>> great mystery? Weird... :)
>>>>>
>>>>> Thanks again guys.
>>>>>
>>>> The regex is a valid regex. It's just useless for the data you are
>>>> providing it.
>>>>
>>>> I'm wondering what is missing from your output? The var_dump says the
>>>> text is a 41 character string, but the data you provided only has 36
>>>> characters in it.
>>>>
>>>> What elements of the string do you want to capture?
>>>>
>>>> /:(\d++)/ would catch the number after the :
>>>>
>>>>
>>>>
>>> Apologies for the delay.
>>>
>>> That works, except it will catch all instances. I need it to catch the
>>> last number specifically and not the period. The output will also output a
>>> previous and new reading- I need the new reading. And I also need to allow
>>> for the possibility of a period. So.... here's what I have:
>>>
>>> /(\d++)[^.]?$
>>>
>>> Which works as long as there is no period at the end. I just can't seem
>>> to get my head around it. I need a guru... :)
>>
>> Did you miss the ":" at the beginning of RIchard's suggested regex?
>> /:(\d++)/ would gather up all digits following a ":". If the output from the
>> mixer command is always something like dd:dd (irrespective of trailing
>> period), the regex above would always return the second set of digits.
>>
>>
> Sorry I probably didn't make it clear. The output can be the above or
> "Setting the mixer vol from 70:70 to 75:75." Hence the $ for end of line. So
> I obviously need the last number (75) and nothing else.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Get the last number.

(\d++)[^\d]*?$

Options: case insensitive; ^ and $ match at line breaks

Match the regular expression below and capture its match into
backreference number 1 Â(\d++)Â
   Match a single digit 0..9 Â\d++Â
      Between one and unlimited times, as many times as possible,
without giving back (possessive) Â++Â
Match a single character that is not a digit 0..9 Â[^\d]*?Â
   Between zero and unlimited times, as few times as possible,
expanding as needed (lazy) Â*?Â
Assert position at the end of a line (at the end of the string or
before a line break character) Â$Â


Created with RegexBuddy


if (preg_match('/(\d++)[^\d]*?$/sim', $subject, $matches)) {
	$result = $matches[0];
} else {
	$result = "";
}


-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux