Using regex to extract numbers and characters from a string

I have a line with text, numbers and symbols. I am trying to extract numbers and characters from a string with limited success. Instead of getting all the numbers and characters, I get only part of it. Below I will explain my regular expression in order to make it more understandable and understandable.

\d : any number
[+,-,*,/,0-9]+ : 1 or more of any +,-,*,/, or number
\d : any number

Code:

$string = "text 1+1-1*1/1= text";

$regex = "~\d[+,-,*,/,0-9]+\d~siU";
preg_match_all($regex, $string, $matches); 

echo $matches[0][0];

Expected results

1+1-1*1/1

Actual Results

1+1
+4
source share
3 answers

U. , + . , . ( 1 ,, . -, ,

+2

, . [+,-,*,/,0-9]. , . , -, . , " ", "", " " - ". " /". \d[+\-*/0-9]+\d .

+2

, :)

((?:[0-9]+[\+|\-|\*|\/]?)+)

, : https://regex101.com/r/mF0zO8/2

-3

Source: https://habr.com/ru/post/1609546/


All Articles