0% found this document useful (0 votes)
6 views3 pages

14.regular Expression

Regular

Uploaded by

Shuwani Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

14.regular Expression

Regular

Uploaded by

Shuwani Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Regular expressions (regex or regexp) are formal expressions used to define patterns in text.

They are widely used in programming, text processing, and data validation for matching and
manipulating strings. Regular expressions provide a powerful way to describe sets of strings that
conform to certain patterns.

Components of Regular Expressions

1. Literal Characters: Match themselves exactly.


o Example: a matches the character 'a'.
2. Special Characters: Define more complex patterns. They include:
o Dot (.): Matches any single character except a newline.
 Example: a.c matches 'abc', 'a1c', 'a$c', etc.
o Backslash (\): Escapes special characters, so they are treated as literals.
 Example: \. matches a literal dot, not any character.
o Caret (^): Matches the start of a string.
 Example: ^a matches 'a' at the start of a string.
o Dollar Sign ($): Matches the end of a string.
 Example: a$ matches 'a' at the end of a string.
3. Character Classes: Define sets of characters.
o Square Brackets [ ]: Match any single character within the brackets.
 Example: [abc] matches 'a', 'b', or 'c'.
o Range: Specify a range of characters.
 Example: [a-z] matches any lowercase letter.
o Negation: Match any character not in the set.
 Example: [^0-9] matches any non-digit character.
4. Predefined Character Classes:
o \d: Matches any digit (equivalent to [0-9]).
o \D: Matches any non-digit character.
o \w: Matches any word character (alphanumeric plus underscore, equivalent to [a-
zA-Z0-9_]).
o \W: Matches any non-word character.
o \s: Matches any whitespace character (space, tab, newline, etc.).
o \S: Matches any non-whitespace character.
5. Quantifiers: Specify how many times an element should be matched.
o Asterisk (*): Matches 0 or more occurrences of the preceding element.
 Example: a* matches '', 'a', 'aa', 'aaa', etc.
o Plus (+): Matches 1 or more occurrences of the preceding element.
 Example: a+ matches 'a', 'aa', 'aaa', etc.
o Question Mark (?): Matches 0 or 1 occurrence of the preceding element (makes
it optional).
 Example: a? matches '' or 'a'.
o Braces ({n,m}): Matches between n and m occurrences of the preceding element.
 Example: a{2,4} matches 'aa', 'aaa', or 'aaaa'.
6. Grouping and Capturing:
o Parentheses (()): Group patterns and capture matched substrings.
 Example: (abc)+ matches one or more repetitions of 'abc'.
o Non-capturing Group (?:...): Groups patterns without capturing the matched
substring.
 Example: (?:abc)+ matches 'abcabc', but does not capture 'abc'.
7. Alternation: Matches one of several patterns.
o Pipe (|): Represents a choice between patterns.
 Example: a|b matches 'a' or 'b'.
8. Anchors: Define positions in the string.
o ^: Start of a string.
o $: End of a string.
o \b: Word boundary.
o \B: Non-word boundary.

Examples

1. Basic Matching:
o cat: Matches the string 'cat'.
o dog|cat: Matches 'dog' or 'cat'.
2. Character Classes and Quantifiers:
o [a-z]: Matches any lowercase letter.
o [0-9]+: Matches one or more digits.
o a{3,5}: Matches 'aaa', 'aaaa', or 'aaaaa'.
3. Complex Patterns:
o ^\d{3}-\d{2}-\d{4}$: Matches a social security number in the format '123-45-
6789'.
o (\d{2}/\d{2}/\d{4}): Matches dates in the format 'dd/mm/yyyy' and captures
the date.

Regular Expression Engines

Different programming languages and tools implement regular expressions with slight variations
in syntax and capabilities. Some well-known implementations include:

 Perl-Compatible Regular Expressions (PCRE): Common in many languages and tools.


 JavaScript: Used in web development.
 Python: Provides the re module.
 Java: Uses the java.util.regex package.
 Ruby: Supports regex through its built-in mechanisms.

Applications

1. Text Search: Finding specific patterns in text files or strings.


2. Text Validation: Checking if input data conforms to certain formats (e.g., email
addresses, phone numbers).
3. String Replacement: Modifying parts of strings based on patterns.
4. Syntax Highlighting: Coloring code in editors based on patterns.

You might also like