Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex Help with manipulating string

Tags:

regex

php

i am seriously struggling to get my head around regex.

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

i want to extract the two numbers after the colon into two seperate strings so delimited by the comma.

Can anyone help me? Cheers

like image 207
user310070 Avatar asked Apr 06 '10 13:04

user310070


People also ask

Is regex used for different string operations?

Regular Expressions (a.k.a regex) are a set of pattern matching commands used to detect string sequences in a large text data. These commands are designed to match a family (alphanumeric, digits, words) of text which makes then versatile enough to handle any text / string class.

What are the benefits of regex?

Benefits of using Regular Expression Supported by almost any language, there are only a few programming languages which do not understand regular expressions; Do more with less, keep your code cleaner; Faster validations, instead of having many IF and ELSE operators you may validate only once with a regular expression.

What are the benefits of regex regular expressions in Python?

A Regular Expression is used for identifying a search pattern in a text string. It also helps in finding out the correctness of the data and even operations such as finding, replacing and formatting the data is possible using Regular Expressions.

Is regex faster than string replace?

String operations will always be faster than regular expression operations. Unless, of course, you write the string operations in an inefficient way. Regular expressions have to be parsed, and code generated to perform the operation using string operations.


1 Answers

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 just:

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

if the string only contains one coordinate.

A small explanation:

\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
like image 86
Bart Kiers Avatar answered Oct 03 '22 18:10

Bart Kiers