Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

24. Regular Expressions (POSIX)

Lesson 21 of 29 in the free PHP Programming notes on Siksha Sarovar, written by Rohit Jangra.

Regular Expressions (POSIX)

A Regular Expression (RegEx) is a pattern used to search or match strings.

Exam Definition: Regular expressions are patterns used for string matching and validation.

---

Regular Expression Syntax (POSIX)

POSIX regular expressions use character classes and operators.

Basic POSIX Symbols

SymbolMeaning
^Start of string
$End of string
.Any character
*Zero or more
+One or more
?Zero or one
[]Character class
[^ ]Negation
{n}Exact count
{n,m}Range

Character Classes

PatternMeaning
[a-z]Lowercase letters
[A-Z]Uppercase letters
[0-9]Digits
[a-zA-Z]Alphabets

Example

/^[a-zA-Z]+$/

✔ Only alphabets allowed

---

PHP’s Regular Expression Functions (POSIX Extended)

PHP supports POSIX Extended RegEx using built-in functions.

Important POSIX Functions

FunctionPurpose
ereg()Match pattern
eregi()Case-insensitive match
ereg_replace()Replace text
split()Split string
Note (Exam-Oriented): POSIX functions are deprecated, but still important for theory and exams.

1. ereg() Example

if(ereg("^[0-9]+$", "12345")){
   echo "Valid Number";
}

2. eregi() Example

if(eregi("php", "PHP Programming")){
   echo "Matched";
}

3. ereg_replace() Example

echo ereg_replace("PHP", "Java", "PHP is easy");

4. split() Example

$arr = split(",", "Red,Green,Blue");

---

POSIX vs PCRE (Exam Comparison)

FeaturePOSIXPCRE
SyntaxSimpleAdvanced
SpeedSlowerFaster
Functionsereg()preg_match()
StatusDeprecatedRecommended
Exam Line: PCRE is preferred over POSIX in modern PHP.

---

Advantages of Regular Expressions

  • Powerful string validation
  • Reduces code length
  • Accurate pattern matching
  • Useful in form validation