How to Match decimal numbers and currency in Regex.mp4

 



Download How to Match decimal numbers and currency in Regex.mp4


Here’s a simple and easy explanation of how to match decimal numbers and currency values using Regex, along with clear examples.


✔️ Matching Decimal Numbers & Currency in Regex

Image

Image


🟦 1. Matching Decimal Numbers

Decimal numbers are numbers that may have a decimal point.

Examples:

12
12.5
0.99
.75
150.00

✅ Basic Regex for Decimal Numbers

\d+(\.\d+)?

✔️ Explanation

  • \d+ → one or more digits

  • (.\d+)? → optional decimal part

    • \. → the dot

    • \d+ → digits after the dot

    • ? → the whole decimal part is optional

✔️ Matches

  • 12

  • 12.5

  • 150.00

❌ Does NOT match

  • .75 (because it requires digits before the dot)


🟦 2. Matching Decimals With Optional Leading Zero

If you want to allow numbers like .75, use this:

✅ Regex

\d*(\.\d+)?

✔️ Matches

  • .75

  • 0.75

  • 12.9

  • 12


🟦 3. Allowing Either Integer or Decimal, but Not Empty

This is often used in real applications.
You want either:

  • digits only → 45

  • digits + decimals → 45.90

  • dot + decimals → .90

✅ Better Regex

\d+(\.\d+)?|\.\d+

🟦 4. Matching Currency Values (₹, $, €, etc.)

Currency usually has:

  • Symbol → $, , , £

  • Decimal part with exactly 2 digits → .00, .50, .99

  • Optional commas → 1,000.50

Examples:

$10.99
₹1,200.00
€0.50
£99

✔️ 4.1 Currency With Symbol & Optional Decimal

✅ Regex

[$₹€£]\d+(\.\d{2})?

✔️ Explanation

  • [$₹€£] → matches one symbol

  • \d+ → the number

  • (\.\d{2})? → optional .00 decimal part with 2 digits

✔️ Matches

  • $10

  • $10.99

  • ₹50.00

  • €9.50


✔️ 4.2 Currency With Commas (1,000.00)

✅ Regex

[$₹€£]\d{1,3}(,\d{3})*(\.\d{2})?

✔️ Explanation

  • \d{1,3} → first group (1–3 digits)

  • (,\d{3})* → optional groups of ,000

  • (.\d{2})? → optional decimal part

✔️ Matches

  • $1,000.00

  • ₹25,50,000.99 (Indian numbering also works depending on format)

  • €999.50


🟩 5. Currency Without Symbol (just numbers like 10.99)

If you want to match only valid currency decimals, exactly 2 digits:

✅ Regex

\d+(\.\d{2})?

✔️ Matches:

  • 10

  • 10.99

❌ Does NOT match:

  • 10.9

  • 10.999


🟩 6. Complete Universal Regex

Match currency numbers with optional:

  • symbol

  • commas

  • decimals

✅ Universal Regex

([$₹€£])?\d{1,3}(,\d{3})*(\.\d{2})?

🎉 Summary (Super Simple)

Purpose Regex
Basic decimal \d+(\.\d+)?
Allow .75 \d*(\.\d+)?
Currency with symbol [$₹€£]\d+(\.\d{2})?
Currency with commas [$₹€£]\d{1,3}(,\d{3})*(\.\d{2})?
Strict 2-decimal currency \d+(\.\d{2})?

Enjoy! Follow us for more... 

No comments:

Post a Comment

How to Match decimal numbers and currency in Regex.mp4

  Download  How to Match decimal numbers and currency in Regex.mp4 Here’s a simple and easy explanation of how to match decimal numbers and...