PHP - Regex - How to Extract a Number with Decimal (Dot and Comma) from a String (E.G. 1,120.01)

Regex PHP Decimal Number with point and operator

You don't even need to use explode() here, because preg_match_all can handle doing multiple regex matches across a given input string:

$liste = "Pack Eclairage : Lave-phares Projecteurs Bi-X\u00e9non 35W directionnels Feux AV eclairage diurne LED : -400.00Etriers de freins Rouge : +200.00|Jantes en alliage 19\" Performance Noires (avec Pneus Run-On Flat) : +900.00|Pack Confort : Syst\u00e8me d'entr\u00e9e sans cl\u00e9s (conducteur et passager) Eclairage des poign\u00e9es de portes Compartiment porte-objets c\u00f4t\u00e9 conducteur Pare-soleil conducteur avec illumination : +580.00|Peinture Pastel Extra-s\u00e9rie Blanc Alfa : +650.00|Vitres AR privatives : +390.00";
preg_match_all ("/([+\-])(\d+(\.\d+)?)/", $liste, $array);

for ($i=0; $i < count($array[1]); $i++) {
echo $array[1][$i] . ", " . $array[2][$i] . "\n";
}

-, 400.00
+, 200.00
+, 900.00
+, 580.00
+, 650.00
+, 390.00

Demo

Regex - Select all numbers without commas

You can use preg_replace

Try this

$str = '- 602,135 results';
echo $result = preg_replace("/[^0-9]/","",$str);

Output - 602135

You can also use to get same output:-

$result = preg_replace('/\D/', '', $str);

Extract a single (unsigned) integer from a string

$str = 'In My Cart : 11 12 items';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);

Regex of max 5 decimal separate by +

You can use

^\d+(?:,\d{1,2})?(?:\+\d+(?:,\d{1,2})?){0,4}$

In the HTML pattern attribute use it as

pattern="\d+(?:,\d{1,2})?(?:\+\d+(?:,\d{1,2})?){0,4}"

See the regex demo.

NOTE: If you want to limit the number of digits in the integer part to be max 3, replace the \d+ with \d{1,3}:

^\d{1,3}(?:,\d{1,2})?(?:\+\d{1,3}(?:,\d{1,2})?){0,4}$

Details:

  • ^ - start of string (implicit in pattern regex)
  • \d+(?:,\d{1,2})? - one or more digits and then an optional sequence of a , and one or two digits
  • (?:\+\d+(?:,\d{1,2})?){0,4} - zero to four occurrences of a + char followed with one or more digits and then an optional sequence of a , and one or two digits
  • $ - end of string (implicit in pattern regex)

C# Extract Decimal number with out period

It seems you just want to extract 1 or more digits after a dot. You may use a simple \.(\d+) regex that matches a literal dot and then matches and captures 1 or more digits into Group 1.

Use

var m = Regex.Match(formula, @"\.(\d+)");
var res = string.Empty;
if (m.Success)
{
res = m.Groups[1].Value;
}

See the regex demo

Sample Image

Find position of first letter in string

You mean this?

<?php
preg_match('/[a-z]+/i', '|Tel: (520) 626-1855 kafds r', $matches, PREG_OFFSET_CAPTURE);
var_export($matches);
?>

Output:

array (
0 =>
array (
0 => 'Tel',
1 => 1,
),
)

To find the position of the string kafds r,

<?php
preg_match('/(?<=\d )\w+ \w+/i', '|Tel: (520) 626-1855 kafds r', $matches, PREG_OFFSET_CAPTURE);
var_export($matches);
?>

Output:

array (
0 =>
array (
0 => 'kafds r',
1 => 21,
),
)

Setting a relevant expression match to false in a do...while

Simple coding bug...comments added to your code to explain issue

if (tollResponse == "yes")
{
Console.WriteLine("How much do you pay per trip?");
string tollTax = Console.ReadLine();
//toll.Success gets set here.
Match toll = Regex.Match(tollTax, @"[\d .]+");

if (toll.Success)
{
//Not sure why you are doing this since you aren't using it in the given code
Math.Round(Convert.ToDecimal(tollTax), 2);
Console.WriteLine("Good lord that's high... well it's your money");
}
else
{
//This is an infinite loop because toll.Success is never set again.
do
{
Console.WriteLine("Please enter a proper number");
tollTax = Console.ReadLine();
} while (toll.Success == false);
}
}

What i think you want

if (tollResponse == "yes")
{
Console.WriteLine("How much do you pay per trip?");

//Loop over the Console.ReadLine() using the else statement and exit if it is right the first time
do
{
string tollTax = Console.ReadLine();
//toll.Success gets set here.
Match toll = Regex.Match(tollTax, @"^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?$");

if (toll.Success)
{
Console.WriteLine("Good lord that's high... well it's your money");
//will exit
}
else
{
Console.WriteLine("Please enter a proper number");
}
} while (toll.Success == false);
}

Note: Removed 1 line of duplicate code as well, updated to use my recommended regex and removed Math.Round


Regex Tool

[\d .]+

Regular expression visualization

Debuggex Demo

A more Valid Regex for currency

decimal optional (two decimal places)

^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?$

Regular expression visualization

Debuggex Demo

Explained:

number (decimal required)

^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*\.[0-9]{2}$

Options: case insensitive

Assert position at the beginning of the string «^»
Match a single character present in the list below «[+-]?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
The character “+” «+»
The character “-” «-»
Match a single character in the range between “0” and “9” «[0-9]{1,3}»
Between one and 3 times, as many times as possible, giving back as needed (greedy) «{1,3}»
Match the regular expression below «(?:,?[0-9]{3})*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character “,” literally «,?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match a single character in the range between “0” and “9” «[0-9]{3}»
Exactly 3 times «{3}»
Match the character “.” literally «\.»
Match a single character in the range between “0” and “9” «[0-9]{2}»
Exactly 2 times «{2}»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»

Will Match:

1,432.01
456.56
654,246.43
432
321,543
14239729
21379312.32

Will not Match

324,123.432
,,,312,.32
123,.23

taken from my answer here php - regex - how to extract a number with decimal (dot and comma) from a string (e.g. 1,120.01)?



Related Topics



Leave a reply



Submit