⊗ppPmRgChS 231 of 447 menu

Character Sets in PHP Regular Expressions

The character groups \d and \w are not very flexible. Even a simple task, like find all letters, but not digits - cannot be solved by them. For such tasks, one should use square brackets, which represent the OR operation.

Square brackets replace one character, any of those listed inside. For example, like this: #x[abc]x# - we say that there must be the letter x on the edges, and inside - one character: either 'a', or 'b', or 'c'.

After square brackets, you can write repetition operators. For example, like this: #x[abc]+x# - we say that inside the x's there can be any number of characters 'a', 'b' and 'c' - in any combinations.

You can not only list characters, but also create character groups by writing a hyphen between two characters. For example, like this: #[a-d]# - we get all characters from 'a' to 'd'.

Let's look at some examples.

Example

In this example, the search pattern looks like this: between the x's any letter from 'a' to 'z':

<?php $str = 'xax xbx xcx x@x'; $res = preg_replace('#x[a-z]x#', '!', $str); ?>

As a result, the following will be written to the variable:

'! ! ! x@x'

Example

In this example, the search pattern looks like this: between the x's any letter from 'a' to 'k':

<?php $str = 'xax xbx xmx x@x'; $res = preg_replace('#x[a-k]x#', '!', $str); ?>

As a result, the following will be written to the variable:

'! ! xmx x@x'

Example

In this example, the search pattern looks like this: between the x's any letter from 'A' to 'Z':

<?php $str = 'xax xBx xcx x@x'; $res = preg_replace('#x[A-Z]x#', '!', $str); ?>

As a result, the following will be written to the variable:

'xax ! xcx x@x'

Example

In this example, the search pattern looks like this: between the x's any digit from 0 to 9:

<?php $str = 'xax x1x x3x x5x x@x'; $res = preg_replace('#x[0-9]x#', '!', $str); ?>

As a result, the following will be written to the variable:

'xax ! ! ! x@x'

Example

In this example, the search pattern looks like this: between the x's any digit from 3 to 7:

<?php $str = 'xax x1x x3x x5x x@x'; $res = preg_replace('#x[3-7]x#', '!', $str); ?>

As a result, the following will be written to the variable:

'xax x1x ! ! x@x'

Example

In this example, the search pattern looks like this: between the x's any letter from 'a' to 'z' or a digit from 1 to 9:

<?php $str = 'xax x1x x3x x5x x@x'; $res = preg_replace('#x[a-z1-9]x#', '!', $str); ?>

As a result, the following will be written to the variable:

'! ! ! ! x@x'

Example

In this example, the search pattern looks like this: between the x's any letter from 'a' to 'z' or a letter from 'A' to 'Z':

<?php $str = 'xax xBx xcx x5x x@x'; $res = preg_replace('#x[a-zA-Z]x#', '!', $str); ?>

As a result, the following will be written to the variable:

'! ! ! x5x x@x'

Example

In this example, the search pattern looks like this: between the x's any letter from 'a' to 'z' or digits 1, 2:

<?php $str = 'xax xbx x1x x2x x3x'; $res = preg_replace('#x[a-z12]x#', '!', $str); ?>

As a result, the following will be written to the variable:

'! ! ! ! x3x'

Example

In this example, the search pattern looks like this: between the x's letters from 'a' to 'z' in the amount of 1 or more:

<?php $str = 'xx xabesx xaadx x123x xa3x'; $res = preg_replace('#x[a-z]+x#', '!', $str); ?>

As a result, the following will be written to the variable:

'xx ! ! ! x123x xa3x'

Example

Let's make it so that the number of letters can be zero:

<?php $str = 'xx xabesx xaadx x123x xa3x'; $res = preg_replace('#x[a-z]*x#', '!', $str); ?>

As a result, the following will be written to the variable:

'! ! ! x123x xa3x'

Practical Tasks

Given a string:

<?php $str = 'aba aea aca aza axa'; ?>

Write a regular expression that finds by the following pattern: the letter 'a' is on the edges, and between them is the letter 'b', 'e' or 'x'.

Given a string:

<?php $str = 'a1a a3a a7a a9a aba'; ?>

Write a regular expression that finds by the following pattern: the letter 'a' is on the edges, and between them is a digit from the 3-rd to the 6-th.

Given a string:

<?php $str = 'aba aea afa aha aga'; ?>

Write a regular expression that finds by the following pattern: the letter 'a' is on the edges, and between them is a letter from a to g.

Given a string:

<?php $str = 'aba aea afa aha aga'; ?>

Write a regular expression that finds by the following pattern: the letter 'a' is on the edges, and between them is a letter from a to f and from j to z.

Given a string:

<?php $str = 'aAa aea aEa aJa a3a'; ?>

Write a regular expression that finds by the following pattern: the letter 'a' is on the edges, and between them is a letter from a to f and from A to D.

Given a string:

<?php $str = 'aAXa aeffa aGha aza ax23a a3sSa'; ?>

Write a regular expression that finds by the following pattern: the letter 'a' is on the edges, and between them are lowercase Latin letters, without affecting the others.

Given a string:

<?php $str = 'aAXa aeffa aGha aza ax23a a3sSa'; ?>

Write a regular expression that finds by the following pattern: the letter 'a' is on the edges, and between them are lowercase and uppercase Latin letters, without affecting the others.

Given a string:

<?php $str = 'aAXa aeffa aGha aza ax23a a3sSa'; ?>

Write a regular expression that finds by the following pattern: the letter 'a' is on the edges, and between them are lowercase Latin letters and digits, without affecting the others.

byenru