www.xbdev.net
xbdev - software development
Monday October 21, 2024
Home | Contact | Support | Programming.. More than just code .... | RegEx (Regular Expressions)... Powerful, expressive and flexible.. ..
     
 

RegEx (Regular Expressions)...

Powerful, expressive and flexible.. ..

 


RegEx > Programming RegEx (Javascript, PHP, ...)


Regular Expressions are great! You can use the in lots of editors like `notepad++' or `visual studio' - there are even a whole range of online tools, like https://regexr.com/. However, there will be times when you'll want to use regular expression with your own languages and tools. Of course, they're supported by everyone and everywhere.

Let's take a look at a few popular languages like 'Python', 'Javascript' and 'PHP' and how you'd go about using regular expressions - not just for `finding' but also for replacing and inserting.


Using Regular Expressions in JavaScript

Creating a Regular Expression

Method Example
RegExp Constructor let regex = new RegExp("pattern");
Literal Notation let regex = /pattern/;

Basic Methods

Method Example
test() let result = regex.test(str);
exec() let result = regex.exec(str);

String Methods

Method Example
match() let result = str.match(regex);
replace() let newStr = str.replace(regex, "replacement");
split() let result = str.split(regex);

Flags

Flag Description Example
i Case-insensitive matching /pattern/i
g Global matching /pattern/g
m Multiline matching /pattern/m

Example

    
let pattern = /apple/gi;
let str = "Apple is a fruit. I love eating apples!";
let result = str.match(pattern); // ["Apple", "apples"]
    

Regular expressions in JavaScript are a powerful tool for pattern matching and manipulation in strings. This overview covers the basics, and you can explore more advanced features as you become more familiar with regex.




Using Regular Expressions in Python

Importing the `re` Module

Method Example
Import import re

Creating a Regular Expression

Method Example
Compile pattern = re.compile(r'your_pattern_here')

Basic Methods

Method Example
search() match = pattern.search(text)
match() match = pattern.match(text)
findall() matches = pattern.findall(text)

Other Methods

Method Example
finditer() matches = pattern.finditer(text)
sub() new_text = pattern.sub("replacement", text)
split() words = pattern.split(text)

Flags

Flag Description Example
re.IGNORECASE Case-insensitive matching pattern = re.compile(r'pattern', re.IGNORECASE)

Examples

    
import re

pattern = re.compile(r'\b\w{3}\b')
text = "The cat is on the mat."
matches = pattern.findall(text)
print("Three-letter words:", matches)
    
  

Regular expressions in Python are versatile and can be applied to a wide range of tasks involving text processing. The re module documentation provides more details and options for advanced usage.




Using Regular Expressions in PHP

1. Using preg_match() for Simple Pattern Matching

Function Example
preg_match()
          
$pattern = '/apple/';
$text = 'I like apples and oranges.';

if (preg_match($pattern, $text)) {
    echo 'Pattern found!';
} else {
    echo 'Pattern not found.';
}
          
        

2. Using preg_match_all() to Find All Matches

Function Example
preg_match_all()
          
$pattern = '/\d+/';
$text = 'There are 42 apples and 7 oranges.';

if (preg_match_all($pattern, $text, $matches)) {
    print_r($matches[0]); // Array containing all matches
} else {
    echo 'No matches found.';
}
          
        

3. Using preg_replace() for Pattern Replacement

Function Example
preg_replace()
          
$pattern = '/apple/';
$text = 'I have an apple.';
$replacement = 'orange';

$newText = preg_replace($pattern, $replacement, $text);
echo $newText; // Output: "I have an orange."
          
        

4. Using preg_split() for String Splitting

Function Example
preg_split()
          
$pattern = '/\s+/';
$text = 'This is a sentence.';
$words = preg_split($pattern, $text);

print_r($words); // Array containing individual words
          
        

5. Using Regular Expression Flags

Function Example
Flags
          
$pattern = '/apple/i'; // Case-insensitive matching
$text = 'I like Apples and Oranges.';

if (preg_match($pattern, $text)) {
    echo 'Pattern found!';
} else {
    echo 'Pattern not found.';
}
          
        

6. Using Capture Groups

Function Example
preg_match() with Capture Groups
          
$pattern = '/(\d{2})-(\d{2})-(\d{4})/';
$text = 'Date of birth: 12-31-2000';

if (preg_match($pattern, $text, $matches)) {
    echo 'Day: ' . $matches[1] . ', Month: ' . $matches[2] . ', Year: ' . $matches[3];
} else {
    echo 'Pattern not found.';
}
          
        

7. Escaping Special Characters

Function Example
preg_quote()
          
$pattern = '/[.*+?^${}()|\[\]\]/';
$escapedPattern = preg_quote('[.*+?^${}()|[\]', '/');
echo $escapedPattern; // Output: \[\.\*\+\?\^$\{\}\(\)\|\[\\]
          
        

These examples cover the basics of using regular expressions in PHP.






 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.