What good is a Fatherland ( b) in a regular expression?

Asked

Viewed 1,975 times

15

A long time ago, I took a look at the use of Boundary, studying about regular expressions in PHP (PCRE Patterns).

I came across the \b (which is called Boundary).

What is the use of it in a regular expression?

1 answer

23


The \b (beta) is an anchor just like his cousins ^(alpha) and $(omega). Once added on one side of the regex will capture pattern specified at start, end or word is valid only for letters, numbers and the underline ([A-Za-z0-9_]) which is the equivalent of \w.

Entree:

dialogo dia melodia diafragma dialeto radial bom-dia

At first \bdia capture strings(words) that contain at the beginning(not the beginning of the line) the pattern dia, the items entering the capture are, daysoon, day, dayfragma, dayLithium and good-day

Anchor at the end dia\b captures strings(words) where the specified pattern is at the end, the captured items are: day, Meloday and good-day

Anchor \bdia\b capture strings(words) extas day and good-day

References

http://www.rexegg.com/regex-boundaries.html

http://www.regular-expressions.info/wordboundaries.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.