Showing posts with label Regex. Show all posts
Showing posts with label Regex. Show all posts

Backreferences to Optional Expressions in Regex.mp4

 


Download Backreferences to Optional Expressions in Regex.mp4


Here’s a simple and easy explanation of Backreferences to Optional Expressions in Regex, with clear examples.


⭐ Backreferences to Optional Expressions (Explained Simply)

A backreference in regex lets you reuse a previously matched group.
Example: \1 means “match exactly what Group 1 matched.”

But what happens when the group you're referencing is optional?

Example of optional group: (abc)?
The ? means → “this group may appear once or not appear at all.”

So a backreference to an optional group means:

  • If the optional group matched something,
    then \1 must match the same text again.

  • If the optional group did NOT match,
    then \1 tries to match… nothing (empty string).

This behavior often confuses beginners.


✔ Example 1 — Optional group appears

Regex:

(abc)?\1

Case 1: string = "abcabc"

  • (abc)? → matches "abc"

  • \1 → must match "abc"

  • ✔ The regex matches.

Case 2: string = "" (empty string)

  • (abc)? → matches nothing

  • \1 → also matches nothing

  • ✔ The regex matches an empty string!

Case 3: string = "abc"

  • (abc)? → matches "abc"

  • \1 → must match "abc"

  • ❌ second "abc" missing → fails


✔ Example 2 — A practical real-world scenario

Match double-quoted strings that might start with a prefix:

Regex:

(prefix:)?".*"\1

Meaning:

  • Optional prefix like: prefix:"value"

  • If prefix exists, the same prefix must repeat at the end

  • If prefix not present, no repeat required

Works for:

✓ prefix:"hello"prefix:
✓ "test"


⚠ Why Backreferences to Optional Groups Are Tricky

Because:

  • If the group is not matched, the backreference evaluates to empty string.

  • This can lead to unexpected matches you didn't intend.

Example:

(a)?b\1

Matches:

  • "aba" → group captured "a" then repeats "a"

  • "b" → group empty, so \1 empty → matches!

This surprises many developers because "b" looks like it shouldn't match.


⭐ Safe Trick: Use “non-empty only” logic

To force a backreference to work only when the optional group actually matched:

Use a conditional (supported in PCRE, .NET, Python regex library):

(a)?b(?(1)a)

Meaning:

  • If group 1 matched → require a

  • If not → require nothing

Now "b" does NOT match, "aba" does match.


🔥 Summary (Very Simple)

Optional Group Backreference Outcome
Group matched text Backreference must match same text Works
Group did not match Backreference matches empty string Works (maybe unexpectedly)


Enjoy! Follow us for more... 

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... 

Explaination about Shorthand character sets.mp4



Download Explanation about Regex shortnand character sets.mp4

 Here’s a simple and clear explanation of the most common Regex shorthand character sets, with easy examples to help you understand how they work.


✅ Regex Shorthand Character Sets (Explained Simply)

Regular expressions (regex) use shorthand character sets to make pattern-matching easier.
Instead of writing long character ranges, you can use short symbols like \d, \w, \s, etc.

Below is a friendly explanation of each.


🔹 1. \d → Digit characters

Meaning: Matches any number from 0–9

Examples:

  • Regex: \d
    Matches: 3 in "A3Z"

  • Regex: \d\d
    Matches: "45" in "Age: 45"


🔹 2. \D → Non-digit characters

Meaning: Matches anything except 0–9

Examples:

  • Regex: \D
    Matches: A in "A3"

  • Regex: \D+
    Matches: "Name" in "Name123"


🔹 3. \w → Word characters

Meaning: Matches

  • Letters (A–Z, a–z)

  • Numbers (0–9)

  • Underscore (_)

Examples:

  • Regex: \w
    Matches: a in "a!"

  • Regex: \w+
    Matches: "Hello123" in "Hello123!!"


🔹 4. \W → Non-word characters

Meaning: Matches anything except letters, numbers, and underscore

Examples:

  • Regex: \W
    Matches: ! in "Hi!"

  • Regex: \W+
    Matches: " @#" in "User @# Name"


🔹 5. \s → Whitespace characters

Meaning: Matches spaces, tabs, and newlines
(space, \t, \n)

Examples:

  • Regex: \s
    Matches the space in "Hello World"

  • Regex: \s+
    Matches " " (multiple spaces)


🔹 6. \S → Non-whitespace characters

Meaning: Matches anything that is not space, tab, or newline

Examples:

  • Regex: \S
    Matches: H in " Hello"

  • Regex: \S+
    Matches: "Hello" in "Hello World"


📌 Additional Character Class Shorthands (POSIX Style)

Some regex engines (like Linux grep -E, PHP, Perl) use POSIX character classes inside [[: ... :]].

Examples:

🔹 [:digit:] → Digits (0–9)

🔹 [:alpha:] → Alphabet letters (A–Z, a–z)

🔹 [:alnum:] → Letters + numbers

🔹 [:upper:] → Uppercase letters

🔹 [:lower:] → Lowercase letters

Example use:

[[:digit:]]+

Matches "2024" in "Year: 2024"


🎯 Summary Table (Quick View)

Shorthand Meaning Example Match
\d Digit 5
\D Not a digit A
\w Word character A, 3, _
\W Not word character !
\s Whitespace (space)
\S Non-whitespace H
[[:digit:]] Digit 7
[[:alpha:]] Letters A, z

Enjoy! Follow us for more... 

How to set-up a Flex project in Flash Builder 4.5.mp4

  Download :  How to set-up a Flex project in Flash Builder 4.5.mp4 How to Set-Up a Flex Project in Flash Builder 4.5 (Beginner Friendly Gui...