Perl angle brackets in regex substitution

Related searches

I have encountered the following code:

my $string = "fo2345obar";
$string =~ s<(\d+)><$1\.>g;

I cannot understand what angle brackets mean in this context; all substitutions I saw before were of form:

$string =~ s/(\d+)/$1\./g;

What do angle brackets mean?

In Perl, you can use any delimiters you like, so the following are all equivalent:


This is possible in sed, too.

But, in Perl, some of the delimiters are special because they come in pairs, e.g.



Substitutions, Using this operator, we can search for regular expressions, and replace all or part of To print this text to the screen, you'd want to leave the angle brackets in place, but to A common perl construct for this sort of task is to use a parenthetic � Substitutions using regular expressions are perhaps the most powerful tool at your disposal when dealing with text. In this primer, Builder AU's Nick Gibson will get you up to speed on using

I'd like to add (to the numerous answers) that there can be two different pairs of delimiters in a s/// statement.

@choroba had an example on s()[] in their answer but didn't draw any particular attention to it, so I want to add this for emphasis.

Consider the regex replacement expression in the form s{aaa}{bbb}.

It's only if the first delimiter is non-paired that it will dictate the delimiter for the entire expression. Then the end-of-match and start-of-replacement delimiters in the middle bleed together (they're the same character), and instead of s#aaa##bbb# you get


On the other hand, if you lead with paired delimiters for the match part, you're free to choose again for the replacement:

# not only

# but also

# and even

This is mostly useful for things like:


to avoid interpolation and escaping in the replacement part, while allowing interpolation in the match.

While this usually makes the code harder to take in (and is therefore not advisable), I'm making a note of it here for completeness's sake - you may come across it in code.

perlre, This page describes the syntax of regular expressions in Perl. made available separately for use later in the pattern, substitution, or program. If you want to match a literal left square bracket, you must escape it, like "\[" . a lookahead/ lookbehind/evaluate zero-width assertion;; a name in angle brackets or single quotes. In this tutorial, we are going to show you how to search and replace strings text using substitution operator s///. We will also introduce you how to use translation operator tr/// to replace character-by-character in strings. Substitution In the previous regular expression tutorials, you have learned how to find the matching text based on a […]

Perl documentation for regex specifies delimiter characters. A few possible cases usage of different delimiters

$string =~ s/pattern/replacement/g;
$string =~ s{pattern}{replacement}g;
$string =~ s!pattern!replacement!g;
$string =~ s|pattern|replacement|g;
$string =~ s<pattern><replacement>g;
$string =~ s#pattern#replacement#g;
$string =~ s'pattern'replacement'g;
$string =~ s,pattern,replacement,g;

Most common used delimiter is / but in some situations it's usage make pattern less readable

$path = '/home/user1/dir1/dir2';

compare these three

$path =~ s/user1\/dir1\/dir2/user2\/dir3\/dir4/;
$path =~ s!user/dir1/dir2!user2/dir3/dir4!;
$path =~ s#user/dir1/dir2#user2/dir3/dir4#;

Please see perlrequick documentation to understand regular expressions and what symbols can be used as delimiters.

Perhaps you will find following website useful in your study perl programming.

Tutorial perl regular expressions

Stackoverflow question 21335765

perlfaq6, How do I substitute case-insensitively on the LHS while preserving case on the RHS? Can I use Perl regular expressions to match balanced text? s{ < # opening angle bracket; (?: # Non-backreffing grouping paren; [^>'"] * # 0 or more � Url Validation Regex | Regular Expression - Taha Match or Validate phone number match whole word nginx test Match html tag Extract String Between Two STRINGS Blocking site with unblocked games Find Substring within a string that begins and ends with paranthesis Empty String Match anything after the specified Checks the length of number and not

The traditional way to change foo to bar in a string is to do this:

$string =~ s/foo/bar/;

This is all well and good, but what if foo and bar contain lots of / characters? For example, what if you want to change all instances of http:// to https://? You'd have to escape out the / characters with \, and so you'd see something like this:

$string =~ s/http:\/\//https:\/\//;

You have to admit that the above substitution is not easy on the eyes. It's not so easy to tell which / belong to s/// and which belong to http://. And the \ characters used to escape the / characters just make the whole line more difficult to understand.

Some people call this "Leaning Toothpick Syndrome," because all the / and \ characters look like leaning toothpicks.

The good news is that you're not required to use / when using s///. There are many other characters you can use instead, which make the following lines equivalent to the one above:

 $string =~ s#http://#https://#;
 $string =~ s@http://@https://@;
 $string =~ s!http://!https://!;
 $string =~ s|http://|https://|;

Because the regular expression delimiter is no longer the default /, you no longer have to escape out the /, making the whole line much easier to read.

You would have to escape out whichever delimiter you use, so to avoid "Leaning Toothpick Syndrome" I would advise using a delimiter that does not appear in your expression.

There are many characters you can use as delimiters; I won't list them here. However, there are four special bracket delimiters that come in pairs and surround your expressions, much like parentheses do. They are: ( and ), [ and ], { and }, and finally, < and >. You use them like this:

$string =~ s(http://)(https://);
$string =~ s<foo><bar>g;

A bit easier to read without all those toothpicks, isn't it?

Your substitution expression will be easier to read if the delimiter you use doesn't exist in the expression itself. (Otherwise, you will have to escape it out with \.)

What's a little strange with the example you gave:

$string =~ s<(\d+)><$1\.>g;

Is that there are no / characters anywhere in the expression, so presumably it's not any clearer than:

$string =~ s/(\d+)/$1\./g;

However, maybe the original coder thought that the two \ characters would contrast with the / characters, and so wanted to avoid the / delimiters altogether.

Note: You didn't ask about this, but the $1\. part of the substitution needlessly escapes out the . character. You don't need the \ there, because the second part of a s/// substitution is not a regular expression -- it is only the replacement, and the . is never used there to match a character. So there's no point in escaping it in your case, as . always means a literal . there.

So the above line would be better written as:

$string =~ s/(\d+)/$1./g;

I hope this helps!

Using Regular Expressions (Learning Perl, 3rd Edition), Using Regular Expressions Substitutions with s/// But the angle brackets ("<" and ">") aren't regular expression metacharacters, so they may not be paired;� To print this text to the screen, you'd want to leave the angle brackets in place, but to insert the string in an html file, you'd need to substitute the symbol &lt; for the left angle bracket (<), and &gt; for the right angle bracket (>). A common perl construct for this sort of task is to use a parenthetic assignment on the left hand side of the regular expression operator; this copies the string to a new location and then modifies the copy, while leaving the original intact.

Perl Tutorial: Regular Expressions (Regex), File IO and Text , Perl is famous for processing text files via regular expressions. the regex . s/ regex/replacement/modifier: Substitute matched substring(s) by the replacement. or write to the underlying file through the filehandle using angle brackets, e.g., � Boost supports the Perl syntax starting with Boost 1.42. Boost adds the Ruby syntax starting with Boost 1.47. To complicate matters, Boost 1.47 allowed these variants to multiply. Boost 1.47 and later allow relative backreferences to be specified with \g or \k and with curly braces, angle brackets, or

Perl's good at text processing, in part due to regular expressions. A regular expression (also regex or regexp ) is a pattern which describes characteristics of a piece of text—to extract an address, replace a misspelling, even to scrape stock prices off of a website to help you figure out what to do with your investment account.

The regular expression to match the balanced text uses two new (to Perl 5.10) regular expression features. These are covered in perlre and this example is a modified version of one in that documentation. First, adding the new possessive + to any quantifier finds the longest match and does not backtrack. That's important since you want to handle any angle brackets through the recursion, not backtracking.