0% found this document useful (0 votes)
13 views

Regex Notes

Uploaded by

Asanov Beksultan
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Regex Notes

Uploaded by

Asanov Beksultan
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

1. Regular expressions in various languages used in web development.

A. JavaScript
=============

We can either create a RegEx object, passing a string as its argument:

let re1 = new RegExp("pattern");

or use the literal:

let re2 = /pattern/;

Testing if a string matches a pattern:

re1.test("string")

returning true or false.

Searching for a pattern:

"string".search(re1);

returning the index of the found match.

Replacing a pattern:

"string".replace("pattern", "what to replace with");

B. C#
=====

We can create an object of type Regex from package


System.Text.RegularExpressions.

It has a method Matches() that accepts a string argument and returns a


collection of matches.

C. PHP
======

There is a preg_match() function.

<?php
preg_match('/(foo)(bar)(baz)/', 'foobarbaz', $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>

D. Python 3
===========

There is a module re, containing, i.a., a function compile() and the Pattern
class.
Pattern objects can be created by:

pattern = compile("pattern")

They have methods match(). search() and replace().

2. Basic regular expressions:

"some text with no special characters" <- this very text

"." <- any character


e.g.: "h.l." <- hole, hill, hell, halt, ...

"[oia]" <- one of the given characters


e.g.: "h[aio]l." <- hole, hill, halt, but NOT hell (a good thing, probably...)

"[a-e]" <- any character from the given RANGE


"[a-z]" <- any lowercase letter
"[a-zA-Z]" <- any letter either upper- or lowercase
"[0-9]" <- any digit
...

(pattern1|pattern2) <- pattern1 or pattern2

"(subpattern)?" <- 0 or 1 occurrence of the subpattern (an optional part of the


pattern)
"(subpattern)*" <- zero or more occurrences
"(subpattern)+" <- one or more occurrences
"(subpattern){n}" <- exactly n occurrences
"(subpattern){n,m}" <- at least n, at most m occurrences
"(subpattern){n,}" <- at least n occurrences
"(subpattern){,m}" <- at most m occurrences

"^pattern" <- a line starting with a pattern


"pattern$" <- a line ending with a pattern

A subpattern can be referred to by using backslash with the number of


subpattern (starting from one in the order of appearing: \1, \2, etc.
For instance:

"([a-z])\1" <- two adjoint identical lowercase letters: aa, bb, cc, ...

There is more of that and there are some `dialects', but the above should be
sufficient to you...

You might also like