String manipulation regex help

I'm seriously trying to raise my head around a regular expression.

I have sring with "iPhone: 52.973053, -0.021447"

I want to extract two numbers after the colon in two separate lines, separated by a comma.

Can anyone help me? Greetings

+5
source share
6 answers

A solution without using regular expressions, using explode()and stripos():

$string = "iPhone: 52.973053,-0.021447";
$coordinates = explode(',', $string);
// $coordinates[0] = "iPhone: 52.973053"
// $coordinates[1] = "-0.021447"

$coordinates[0]  = trim(substr($coordinates[0], stripos($coordinates[0], ':') +1));

Assuming the string always contains a colon.

Or, if the identifier before the colon contains only characters (not numbers), you can also do this:

$string = "iPhone: 52.973053,-0.021447";
$string  = trim($string, "a..zA..Z: ");
//$string = "52.973053,-0.021447"

$coordinates = explode(',', $string);
+2
source

Try:

preg_match_all('/\w+:\s*(-?\d+\.\d+),(-?\d+\.\d+)/',
    "iPhone: 52.973053,-0.021447 FOO: -1.0,-1.0",
    $matches, PREG_SET_ORDER);
print_r($matches);

which produces:

Array
(
    [0] => Array
        (
            [0] => iPhone: 52.973053,-0.021447
            [1] => 52.973053
            [2] => -0.021447
        )

    [1] => Array
        (
            [0] => FOO: -1.0,-1.0
            [1] => -1.0
            [2] => -1.0
        )

)

Or simply:

preg_match('/\w+:\s*(-?\d+\.\d+),(-?\d+\.\d+)/',
    "iPhone: 52.973053,-0.021447",
    $match);
print_r($match);

if the line contains only one coordinate.

:

\w+      # match a word character: [a-zA-Z_0-9] and repeat it one or more times
:        # match the character ':'
\s*      # match a whitespace character: [ \t\n\x0B\f\r] and repeat it zero or more times
(        # start capture group 1
  -?     #   match the character '-' and match it once or none at all
  \d+    #   match a digit: [0-9] and repeat it one or more times
  \.     #   match the character '.'
  \d+    #   match a digit: [0-9] and repeat it one or more times
)        # end capture group 1
,        # match the character ','
(        # start capture group 2
  -?     #   match the character '-' and match it once or none at all
  \d+    #   match a digit: [0-9] and repeat it one or more times
  \.     #   match the character '.'
  \d+    #   match a digit: [0-9] and repeat it one or more times
)        # end capture group 2
+7

Try:

$string = "iPhone: 52.973053,-0.021447";

preg_match_all( "/-?\d+\.\d+/", $string, $result );
print_r( $result );
0

I like the @Felix non-regex solution, I think its solution to the problem is more understandable and understandable than using a regular expression.

Remember that you can use constants / variables to change the separation by comma or colon if the original format of the string is changed.

Sort of

define('COORDINATE_SEPARATOR',',');
define('DEVICE_AND_COORDINATES_SEPARATOR',':');
0
source
$str="iPhone: 52.973053,-0.021447";
$s = array_filter(preg_split("/[a-zA-Z:,]/",$str) );
print_r($s);
0
source

An even simpler solution is to use preg_split () with a much simpler regular expression like

$str   = 'iPhone: 52.973053,-0.021447';
$parts = preg_split('/[ ,]/', $str);
print_r($parts);

which will give you

Array 
(
    [0] => iPhone:
    [1] => 52.973053
    [2] => -0.021447
)
0
source

All Articles