0

I have a string with many lines and one of those lines is a temperature reading and the method used to take the temperature like so:

Example line 1
temp: 35.20c / 95.36f - axillary
Example line 2

Obviously the temp is "35.20c / 95.36f", and the method is "axillary". The method part is optional. I'm having problems writing a REGEX pattern that will extract both since the method can be optional.

So if i run the pattern in a preg_match_all() on the following string:

temp: 35.20c / 95.36f - axillary
temp: 35.20c / 95.36f
temp: 35.20c / 95.36f - oral

I would expect to get a print similar to this:

Array
(
    [0] => Array
        (
            [0] => temp: 35.20c / 95.36f - axillary
            [1] => temp: 35.20c / 95.36f
            [2] => temp: 35.20c / 95.36f - oral
        )

    [1] => Array
        (
            [0] => 35.20c / 95.36f
            [1] => 35.20c / 95.36f
            [2] => 35.20c / 95.36f
        )

    [2] => Array
        (
            [0] => axillary
            [1] => 
            [2] => oral
        )

I have tried many different patterns, so I'll just post my original (which makes sense to me):

$ptn = "/temp: *(.+)(?: - )?(.+)?/";

Sorry guys I guess I need to add some more details:

  • I have no idea what kind of format the temp will be displayed in (35.20c / 95.36f, 35c, 95.3f, etc)
  • I basically just need to take everything after the "temp: " and before the hyphen as my temp and everything after that is going to be my method.
1
  • 2
    Can you post what you have (regex)? I'm sure someone can write one for you, but it may be more helpful to know where you went wrong? Commented Mar 1, 2011 at 21:08

6 Answers 6

1

Try this one:

<?php

    $lines = "temp: 35.20c / 95.36f - axillary
temp: 35.20c / 95.36f
temp: 35.20c / 95.36f - oral";

  preg_match_all("/^temp:\s+([^-\n]+)( - )?(.*)/m", $lines, $matches);

  print_r($matches);

?>
Sign up to request clarification or add additional context in comments.

3 Comments

That works for this particular example, but I can't really rely on the temp being in an particular format - sorry I should have included that in my question.
Ah ok. This one should do that then: $ptn = "/^temp:\s+([^-\n]+)( - )?(.*)/m"
Excellent, all I have to do is strip off the "- " at the beginning of the method. Thank you so much!
1

Ah I think your problem is with (.+) matching everything. Regex patterns are "greedy" and will try and match as much as they possibly can. That pattern matches the rest of the string, leaving nothing for the other groups.

Comments

1

So it looks to me like you want:

/^temp: (\d+\.\d+)c \/ (\d+\.\d+)f(?: - ([^$]+))?$/

The centigrade temp will be in $1, the Fahrenheit version will be in $2, and the method will be in $3. ([^$]+) may not be correct dependiing on what you want to do, since it will capture everything up to the end of the line (like whitespace, if there is any). You could use (?: - ([^$]+?))?\s*$/ at the end instead, I think that would fix that.

Are the temperatures always in a decimal format? Could they ever just be "0c / 32f"?


Edit: Just saw your update. It looks like the greedy .+ is indeed part of the problem, as Rob Agar suggested. You can try this:

/^temp:\s*(\d+(?:\.\d+)?)c\s*\/\s*(\d+(?:\.\d+)?)f(?:\s*-\s*([^$]+?))?\s*$/

That should work even if the method is more than one word. Not sure if that's a possibility, I'm making my best guess at your requirements.

1 Comment

didn't return anything, I'm trying to tinker with that pattern now to get it to work.
0

Basically you need a '?' after the capturing group for the method. That indicates that the group may not be there, but the pattern as a whole should still match. What does your pattern look like at the moment?

Comments

0

I might be making some assumptions here but you could try the following

/^temp: ((\d+\.\d+c) / (\d+\.\d+f))( - (\w+))?$/

Due to all the sub-groupings, your matching array will contain more items than in your example but the one's you're after should be in there

1 Comment

I can't count on the #.##c / #.##f, I basically need to take what ever comes after the temp: and before the hyphen (-) as my temp and everything after the hyphen as my method.
0

$ptn = "/temp: (.)(\s-\s)?(.)/";

1 Comment

thanks, but not really what I'm looking for - Print:code(Array ( [0] => Array ( [0] => temp: 35 [1] => temp: 35 [2] => temp: 35 [3] => temp: 96 ) [1] => Array ( [0] => 3 [1] => 3 [2] => 3 [3] => 9 ) [2] => Array ( [0] => [1] => [2] => [3] => ) [3] => Array ( [0] => 5 [1] => 5 [2] => 5 [3] => 6 ) ) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.