php


PHP regex to extract special string


I am trying to use regex to extract a certain syntax, in my case something like "10.100" or "20.111", in which 2 numbers are separated by dot(.) . So if I provide "a 10.100", it will extract 10.100 from the string. If I provide "a 10.100 20.101", it will extract 10.100 and 20.101.
Until now I have tried to use
preg_match('/^.*([0-9]{1,2})[^\.]([0-9]{1,4}).*$/', $message, $array);
but still no luck. Please provide any suggestion because I don't have strong regex knowledge. Thanks.
You may use
\b[0-9]{1,2}\.[0-9]{1,4}\b
See the regex demo.
Details:
\b - a leading word boundary
[0-9]{1,2} - 1 or 2 digits
\. - a dot
[0-9]{1,4} - 1 to 4 digits
\b - a trailing word boundary.
If you do not care about the whole word option, just remove \b. Also, to match just 1 or more digits, you may use + instead of the limiting quantifiers. So, perhaps
[0-9]+\.[0-9]+
will also work for you.
See a PHP demo:
$re = '/[0-9]+\.[0-9]+/';
$str = 'I am trying to use regex to extract a certain syntax, in my case something like "10.100" or "20.111", in which 2 numbers are separated by dot(.) . So if I provide "a 10.100", it will extract 10.100 from the string. If I provide "a 10.100 20.101", it will extract 10.100 and 20.101.';
preg_match_all($re, $str, $matches);
print_r($matches[0]);
Output:
Array
(
[0] => 10.100
[1] => 20.111
[2] => 10.100
[3] => 10.100
[4] => 10.100
[5] => 20.101
[6] => 10.100
[7] => 20.101
)
Regex: /\d+(?:\.\d+)/
1. \d+ for matching digits one or more.
2. (?:\.\d+) for matching digits followed by . like .1234
Try this code snippet here
<?php
ini_set('display_errors', 1);
$string='a 10.100 20.101';
preg_match_all('/\d+(?:\.\d+)/', $string, $array);
print_r($array);
Output:
Array
(
[0] => Array
(
[0] => 10.100
[1] => 20.101
)
)
$decimals = "10.5 100.50 10.250";
preg_match_all('/\b[\d]{2}\.\d+\b/', $decimals, $output);
print_r($output);
Output:
Array
(
[0] => 10.5
[1] => 10.250
)
Regex Demo | Php Demo

Related Links

LAMP not working as expected. Can login to phpmyadmin but not edit or do anything on remote
Symfony2 / Doctrine : Form : One-To-One nested form delete reference
How to remove php code from showing
broken link in symfony using assetic and angular
preg_replace_callback regex syntax incorrect [closed]
POSTing Data into database using PHP and mysql [duplicate]
ERROR: Could not able to execute INSERT INTO [duplicate]
Split multiple values into two or more arrays
php image loose its quality after resize
Get relation of ManyToMany entities
How can I call shell_exec() from PHP webhook?
Made custom Magento collection, need to show attribute from and entitie's parent entity within foreach loop
Is it possible to preview remote desktop on php website?
Bool is returning an array causing true response every time in Laravel 5.2
no json output given from the server
Laravel REST api PUT / DELETE requests and Content-Type application/json header

Categories

HOME
yahoo-weather-api
yii
redmine
tcsh
code-formatting
hdfs
iverilog
histogram
ipv6
mithril.js
cisco
phpstorm-2017.1
fresco
office-ui-fabric
gmp
nose
nullreferenceexception
libgit2
batch-rename
device
backendless
quartz.net
pyopencl
python-appium
spreadsheetgear
clipboard
miniprofiler
envoy
commonsware-cwac
mongoengine
winrm
busboy
strapi
elastica
suds
raytracing
spongycastle
cloudwatch
renderman
gtk#
android-mediarecorder
sesame
qtextedit
mifos
portal
jsonix
data-management
wininet
deferred-rendering
facebook-ios-sdk
wepay
ajax4jsf
helm
textpattern
variable-length-array
google-maps-api-2
ruby-on-rails-4.1
mousehover
apache-commons-digester
asp.net-web-api-helppages
fantom
abstract-factory
subfolder
incron
string.format
adhoc-polymorphism
cout
smacss
berkeley-db-xml
visual-studio-6
git-reset
jomsocial
principalcontext
colon
insertion
rikulo
notin
donut-chart
red-system
handshake
jsplitpane
instance-variables
struts2-s2hibernate
java.lang.class
series-40
mkannotation
acpi
affinetransform
background-repeat
expression-web
snackjs
botnet
tablet-pc
turbine
hungarian-notation
solandra
chunking
multiple-users
raw-data
self-tracking-entities
oracle-pro-c
rational-unified-process
rawcontacts
gtktextview
comment-conventions

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App