Pakistan Special
PKR Amount (Rs or PKR) Regex
Prices in Pakistan are written in several ways: Rs. 1,500, Rs250, PKR 25,000.50 and so on. This pattern searches for the prefix Rs with an optional dot, or the currency code PKR, followed by an optional space and an amount. The amount allows one to three leading digits, thousands groups separated by commas, and an optional one or two digit decimal part. The i flag accepts rs and pkr in lower case too. It is well suited to pulling totals out of receipts, SMS alerts and order emails. Remove commas from the captured text before converting it to a number.
Pattern (JavaScript)
/\b(?:Rs\.?|PKR)\s?\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?\b/iMatches a word boundary, then (the text "Rs", then a literal ".", optionally (zero or one time) OR the text "PKR"), then a whitespace character (space, tab, newline), optionally (zero or one time), then a digit (0-9), between 1 and 3 times, then (the character ",", then exactly 3 digits), zero or more times, then (the character ".", then a digit (0-9), between 1 and 2 times), optionally (zero or one time), then a word boundary.
Examples
Matches
- ✓ Rs. 1,500
- ✓ PKR 25,000.50
- ✓ Total: Rs250
Does not match
- ✗ $1,500
- ✗ 1,500 rupees
- ✗ Rs. abc
Try it
3 matches (JavaScript engine)
Rs. 1,500 PKR 25,000.50 Total: Rs250 $1,500 1,500 rupees Rs. abc
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /\b(?:Rs\.?|PKR)\s?\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?\b/gi;
const text = "Rs. 1,500";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'\b(?:Rs\.?|PKR)\s?\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?\b', re.I)
text = 'Rs. 1,500'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/\b(?:Rs\.?|PKR)\s?\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?\b/i';
$text = 'Rs. 1,500';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("\\b(?:Rs\\.?|PKR)\\s?\\d{1,3}(?:,\\d{3})*(?:\\.\\d{1,2})?\\b", Pattern.CASE_INSENSITIVE);
String text = "Rs. 1,500";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?i)\b(?:Rs\.?|PKR)\s?\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?\b`)
text := "Rs. 1,500"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"\b(?:Rs\.?|PKR)\s?\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?\b", RegexOptions.IgnoreCase);
var text = "Rs. 1,500";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"(?i)\b(?:Rs\.?|PKR)\s?\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?\b").unwrap();
let text = "Rs. 1,500";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /\b(?:Rs\.?|PKR)\s?\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?\b/i
text = 'Rs. 1,500'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Does it handle amounts written after the number, like 500 PKR?
- No. It expects the prefix before the number. Add a second alternative with the currency after the digits if your data uses that order.
- What about lakh-style grouping such as 1,50,000?
- The pattern assumes groups of three digits. With lakh-style commas it only matches the first part, so remove commas before searching if your source uses that style.
- How do I get the numeric value?
- Take the matched digits, remove commas and parse the result as a number. Use integer paisa or a decimal library for money arithmetic to avoid rounding errors.