{
    "content": [
        {
            "type": "text",
            "text": "# perlrequick (man)\n\n## NAME\n\nperlrequick - Perl regular expressions quick start\n\n## DESCRIPTION\n\nThis page covers the very basics of understanding, creating and using regular expressions\n('regexes') in Perl.\n\n## Sections\n\n- **NAME**\n- **DESCRIPTION** (10 subsections)\n- **BUGS**\n- **SEE ALSO**\n- **AUTHOR AND COPYRIGHT** (1 subsections)\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "perlrequick",
        "section": "",
        "mode": "man",
        "summary": "perlrequick - Perl regular expressions quick start",
        "synopsis": null,
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 7,
                "subsections": [
                    {
                        "name": "Simple word matching",
                        "lines": 93
                    },
                    {
                        "name": "Using character classes",
                        "lines": 107
                    },
                    {
                        "name": "Matching this or that",
                        "lines": 20
                    },
                    {
                        "name": "Grouping things and hierarchical matching",
                        "lines": 14
                    },
                    {
                        "name": "Extracting matches",
                        "lines": 30
                    },
                    {
                        "name": "Matching repetitions",
                        "lines": 40
                    },
                    {
                        "name": "More matching",
                        "lines": 28
                    },
                    {
                        "name": "Search and replace",
                        "lines": 51
                    },
                    {
                        "name": "The split operator",
                        "lines": 30
                    },
                    {
                        "name": "\"use re 'strict'\"",
                        "lines": 5
                    }
                ]
            },
            {
                "name": "BUGS",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 3,
                "subsections": []
            },
            {
                "name": "AUTHOR AND COPYRIGHT",
                "lines": 4,
                "subsections": [
                    {
                        "name": "Acknowledgments",
                        "lines": 4
                    }
                ]
            }
        ],
        "sections": {
            "NAME": {
                "content": "perlrequick - Perl regular expressions quick start\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "This page covers the very basics of understanding, creating and using regular expressions\n('regexes') in Perl.\n\nThe Guide\nThis page assumes you already know things, like what a \"pattern\" is, and the basic syntax of\nusing them.  If you don't, see perlretut.\n",
                "subsections": [
                    {
                        "name": "Simple word matching",
                        "content": "The simplest regex is simply a word, or more generally, a string of characters.  A regex\nconsisting of a word matches any string that contains that word:\n\n\"Hello World\" =~ /World/;  # matches\n\nIn this statement, \"World\" is a regex and the \"//\" enclosing \"/World/\" tells Perl to search a\nstring for a match.  The operator \"=~\" associates the string with the regex match and\nproduces a true value if the regex matched, or false if the regex did not match.  In our\ncase, \"World\" matches the second word in \"Hello World\", so the expression is true.  This idea\nhas several variations.\n\nExpressions like this are useful in conditionals:\n\nprint \"It matches\\n\" if \"Hello World\" =~ /World/;\n\nThe sense of the match can be reversed by using \"!~\" operator:\n\nprint \"It doesn't match\\n\" if \"Hello World\" !~ /World/;\n\nThe literal string in the regex can be replaced by a variable:\n\n$greeting = \"World\";\nprint \"It matches\\n\" if \"Hello World\" =~ /$greeting/;\n\nIf you're matching against $, the \"$ =~\" part can be omitted:\n\n$ = \"Hello World\";\nprint \"It matches\\n\" if /World/;\n\nFinally, the \"//\" default delimiters for a match can be changed to arbitrary delimiters by\nputting an 'm' out front:\n\n\"Hello World\" =~ m!World!;   # matches, delimited by '!'\n\"Hello World\" =~ m{World};   # matches, note the matching '{}'\n\"/usr/bin/perl\" =~ m\"/perl\"; # matches after '/usr/bin',\n# '/' becomes an ordinary char\n\nRegexes must match a part of the string exactly in order for the statement to be true:\n\n\"Hello World\" =~ /world/;  # doesn't match, case sensitive\n\"Hello World\" =~ /o W/;    # matches, ' ' is an ordinary char\n\"Hello World\" =~ /World /; # doesn't match, no ' ' at end\n\nPerl will always match at the earliest possible point in the string:\n\n\"Hello World\" =~ /o/;       # matches 'o' in 'Hello'\n\"That hat is red\" =~ /hat/; # matches 'hat' in 'That'\n\nNot all characters can be used 'as is' in a match.  Some characters, called metacharacters,\nare considered special, and reserved for use in regex notation.  The metacharacters are\n\n{}[]()^$.|*+?\\\n\nA metacharacter can be matched literally by putting a backslash before it:\n\n\"2+2=4\" =~ /2+2/;    # doesn't match, + is a metacharacter\n\"2+2=4\" =~ /2\\+2/;   # matches, \\+ is treated like an ordinary +\n'C:\\WIN32' =~ /C:\\\\WIN/;                       # matches\n\"/usr/bin/perl\" =~ /\\/usr\\/bin\\/perl/;  # matches\n\nIn the last regex, the forward slash '/' is also backslashed, because it is used to delimit\nthe regex.\n\nMost of the metacharacters aren't always special, and other characters (such as the ones\ndelimiting the pattern) become special under various circumstances.  This can be confusing\nand lead to unexpected results.  \"use re 'strict'\" can notify you of potential pitfalls.\n\nNon-printable ASCII characters are represented by escape sequences.  Common examples are \"\\t\"\nfor a tab, \"\\n\" for a newline, and \"\\r\" for a carriage return.  Arbitrary bytes are\nrepresented by octal escape sequences, e.g., \"\\033\", or hexadecimal escape sequences, e.g.,\n\"\\x1B\":\n\n\"1000\\t2000\" =~ m(0\\t2)  # matches\n\"cat\" =~ /\\143\\x61\\x74/  # matches in ASCII, but\n# a weird way to spell cat\n\nRegexes are treated mostly as double-quoted strings, so variable substitution works:\n\n$foo = 'house';\n'cathouse' =~ /cat$foo/;   # matches\n'housecat' =~ /${foo}cat/; # matches\n\nWith all of the regexes above, if the regex matched anywhere in the string, it was considered\na match.  To specify where it should match, we would use the anchor metacharacters \"^\" and\n\"$\".  The anchor \"^\" means match at the beginning of the string and the anchor \"$\" means\nmatch at the end of the string, or before a newline at the end of the string.  Some examples:\n\n\"housekeeper\" =~ /keeper/;         # matches\n\"housekeeper\" =~ /^keeper/;        # doesn't match\n\"housekeeper\" =~ /keeper$/;        # matches\n\"housekeeper\\n\" =~ /keeper$/;      # matches\n\"housekeeper\" =~ /^housekeeper$/;  # matches\n"
                    },
                    {
                        "name": "Using character classes",
                        "content": "A character class allows a set of possible characters, rather than just a single character,\nto match at a particular point in a regex.  There are a number of different types of\ncharacter classes, but usually when people use this term, they are referring to the type\ndescribed in this section, which are technically called \"Bracketed character classes\",\nbecause they are denoted by brackets \"[...]\", with the set of characters to be possibly\nmatched inside.  But we'll drop the \"bracketed\" below to correspond with common usage.  Here\nare some examples of (bracketed) character classes:\n\n/cat/;            # matches 'cat'\n/[bcr]at/;        # matches 'bat', 'cat', or 'rat'\n\"abc\" =~ /[cab]/; # matches 'a'\n\nIn the last statement, even though 'c' is the first character in the class, the earliest\npoint at which the regex can match is 'a'.\n\n/[yY][eE][sS]/; # match 'yes' in a case-insensitive way\n# 'yes', 'Yes', 'YES', etc.\n/yes/i;         # also match 'yes' in a case-insensitive way\n\nThe last example shows a match with an 'i' modifier, which makes the match case-insensitive.\n\nCharacter classes also have ordinary and special characters, but the sets of ordinary and\nspecial characters inside a character class are different than those outside a character\nclass.  The special characters for a character class are \"-]\\^$\" and are matched using an\nescape:\n\n/[\\]c]def/; # matches ']def' or 'cdef'\n$x = 'bcr';\n/[$x]at/;   # matches 'bat, 'cat', or 'rat'\n/[\\$x]at/;  # matches '$at' or 'xat'\n/[\\\\$x]at/; # matches '\\at', 'bat, 'cat', or 'rat'\n\nThe special character '-' acts as a range operator within character classes, so that the\nunwieldy \"[0123456789]\" and \"[abc...xyz]\" become the svelte \"[0-9]\" and \"[a-z]\":\n\n/item[0-9]/;  # matches 'item0' or ... or 'item9'\n/[0-9a-fA-F]/;  # matches a hexadecimal digit\n\nIf '-' is the first or last character in a character class, it is treated as an ordinary\ncharacter.\n\nThe special character \"^\" in the first position of a character class denotes a negated\ncharacter class, which matches any character but those in the brackets.  Both \"[...]\" and\n\"[^...]\" must match a character, or the match fails.  Then\n\n/[^a]at/;  # doesn't match 'aat' or 'at', but matches\n# all other 'bat', 'cat, '0at', '%at', etc.\n/[^0-9]/;  # matches a non-numeric character\n/[a^]at/;  # matches 'aat' or '^at'; here '^' is ordinary\n\nPerl has several abbreviations for common character classes. (These definitions are those\nthat Perl uses in ASCII-safe mode with the \"/a\" modifier.  Otherwise they could match many\nmore non-ASCII Unicode characters as well.  See \"Backslash sequences\" in perlrecharclass for\ndetails.)\n\n•   \\d is a digit and represents\n\n[0-9]\n\n•   \\s is a whitespace character and represents\n\n[\\ \\t\\r\\n\\f]\n\n•   \\w is a word character (alphanumeric or ) and represents\n\n[0-9a-zA-Z]\n\n•   \\D is a negated \\d; it represents any character but a digit\n\n[^0-9]\n\n•   \\S is a negated \\s; it represents any non-whitespace character\n\n[^\\s]\n\n•   \\W is a negated \\w; it represents any non-word character\n\n[^\\w]\n\n•   The period '.' matches any character but \"\\n\"\n\nThe  \"\\d\\s\\w\\D\\S\\W\"  abbreviations  can be used both inside and outside of character classes.\nHere are some in use:\n\n/\\d\\d:\\d\\d:\\d\\d/; # matches a hh:mm:ss time format\n/[\\d\\s]/;         # matches any digit or whitespace character\n/\\w\\W\\w/;         # matches a word char, followed by a\n# non-word char, followed by a word char\n/..rt/;           # matches any two chars, followed by 'rt'\n/end\\./;          # matches 'end.'\n/end[.]/;         # same thing, matches 'end.'\n\nThe word anchor  \"\\b\" matches a boundary between a word character and  a  non-word  character\n\"\\w\\W\" or \"\\W\\w\":\n\n$x = \"Housecat catenates house and cat\";\n$x =~ /\\bcat/;  # matches cat in 'catenates'\n$x =~ /cat\\b/;  # matches cat in 'housecat'\n$x =~ /\\bcat\\b/;  # matches 'cat' at end of string\n\nIn the last example, the end of the string is considered a word boundary.\n\nFor  natural  language  processing (so that, for example, apostrophes are included in words),\nuse instead \"\\b{wb}\"\n\n\"don't\" =~ / .+? \\b{wb} /x;  # matches the whole string\n"
                    },
                    {
                        "name": "Matching this or that",
                        "content": "We can match different character strings with the alternation metacharacter  '|'.   To  match\n\"dog\"  or \"cat\", we form the regex \"dog|cat\".  As before, Perl will try to match the regex at\nthe earliest possible point in the string.  At each character position, Perl will  first  try\nto  match  the first alternative, \"dog\".  If \"dog\" doesn't match, Perl will then try the next\nalternative, \"cat\".  If \"cat\" doesn't match either, then the match fails and  Perl  moves  to\nthe next position in the string.  Some examples:\n\n\"cats and dogs\" =~ /cat|dog|bird/;  # matches \"cat\"\n\"cats and dogs\" =~ /dog|cat|bird/;  # matches \"cat\"\n\nEven  though  \"dog\"  is  the  first  alternative  in the second regex, \"cat\" is able to match\nearlier in the string.\n\n\"cats\"          =~ /c|ca|cat|cats/; # matches \"c\"\n\"cats\"          =~ /cats|cat|ca|c/; # matches \"cats\"\n\nAt a given character position, the first alternative that allows the regex match  to  succeed\nwill  be the one that matches. Here, all the alternatives match at the first string position,\nso the first matches.\n"
                    },
                    {
                        "name": "Grouping things and hierarchical matching",
                        "content": "The grouping metacharacters \"()\" allow a part of a regex to be  treated  as  a  single  unit.\nParts  of  a regex are grouped by enclosing them in parentheses.  The regex house(cat|keeper)\nmeans match \"house\" followed by either \"cat\" or \"keeper\".  Some more examples are\n\n/(a|b)b/;    # matches 'ab' or 'bb'\n/(^a|b)c/;   # matches 'ac' at start of string or 'bc' anywhere\n\n/house(cat|)/;  # matches either 'housecat' or 'house'\n/house(cat(s|)|)/;  # matches either 'housecats' or 'housecat' or\n# 'house'.  Note groups can be nested.\n\n\"20\" =~ /(19|20|)\\d\\d/;  # matches the null alternative '()\\d\\d',\n# because '20\\d\\d' can't match\n"
                    },
                    {
                        "name": "Extracting matches",
                        "content": "The grouping metacharacters \"()\" also allow the extraction of the  parts  of  a  string  that\nmatched.  For each grouping, the part that matched inside goes into the special variables $1,\n$2, etc.  They can be used just as ordinary variables:\n\n# extract hours, minutes, seconds\n$time =~ /(\\d\\d):(\\d\\d):(\\d\\d)/;  # match hh:mm:ss format\n$hours = $1;\n$minutes = $2;\n$seconds = $3;\n\nIn  list  context,  a  match  \"/regex/\" with groupings will return the list of matched values\n\"($1,$2,...)\".  So we could rewrite it as\n\n($hours, $minutes, $second) = ($time =~ /(\\d\\d):(\\d\\d):(\\d\\d)/);\n\nIf the groupings in a regex  are  nested,  $1  gets  the  group  with  the  leftmost  opening\nparenthesis,  $2 the next opening parenthesis, etc.  For example, here is a complex regex and\nthe matching variables indicated below it:\n\n/(ab(cd|ef)((gi)|j))/;\n1  2      34\n\nAssociated with the matching variables $1, $2, ... are the backreferences \"\\g1\",  \"\\g2\",  ...\nBackreferences are matching variables that can be used inside a regex:\n\n/(\\w\\w\\w)\\s\\g1/; # find sequences like 'the the' in string\n\n$1,  $2,  ...  should  only  be  used outside of a regex, and \"\\g1\", \"\\g2\", ... only inside a\nregex.\n"
                    },
                    {
                        "name": "Matching repetitions",
                        "content": "The quantifier metacharacters \"?\", \"*\", \"+\", and \"{}\" allow us to  determine  the  number  of\nrepeats  of  a portion of a regex we consider to be a match.  Quantifiers are put immediately\nafter the character, character class, or grouping that we want to  specify.   They  have  the\nfollowing meanings:\n\n•   \"a?\" = match 'a' 1 or 0 times\n\n•   \"a*\" = match 'a' 0 or more times, i.e., any number of times\n\n•   \"a+\" = match 'a' 1 or more times, i.e., at least once\n\n•   \"a{n,m}\" = match at least \"n\" times, but not more than \"m\" times.\n\n•   \"a{n,}\" = match at least \"n\" or more times\n\n•   \"a{,n}\" = match \"n\" times or fewer\n\n•   \"a{n}\" = match exactly \"n\" times\n\nHere are some examples:\n\n/[a-z]+\\s+\\d*/;  # match a lowercase word, at least some space, and\n# any number of digits\n/(\\w+)\\s+\\g1/;    # match doubled words of arbitrary length\n$year =~ /^\\d{2,4}$/;  # make sure year is at least 2 but not more\n# than 4 digits\n$year =~ /^\\d{ 4 }$|^\\d{2}$/; # better match; throw out 3 digit dates\n\nThese  quantifiers  will try to match as much of the string as possible, while still allowing\nthe regex to match.  So we have\n\n$x = 'the cat in the hat';\n$x =~ /^(.*)(at)(.*)$/; # matches,\n# $1 = 'the cat in the h'\n# $2 = 'at'\n# $3 = ''   (0 matches)\n\nThe first quantifier \".*\" grabs as much of the string as  possible  while  still  having  the\nregex match. The second quantifier \".*\" has no string left to it, so it matches 0 times.\n"
                    },
                    {
                        "name": "More matching",
                        "content": "There  are  a  few  more  things you might want to know about matching operators.  The global\nmodifier \"/g\" allows the matching operator  to  match  within  a  string  as  many  times  as\npossible.   In  scalar  context, successive matches against a string will have \"/g\" jump from\nmatch to match, keeping track of position in the string as it goes along.  You can get or set\nthe position with the pos() function.  For example,\n\n$x = \"cat dog house\"; # 3 words\nwhile ($x =~ /(\\w+)/g) {\nprint \"Word is $1, ends at position \", pos $x, \"\\n\";\n}\n\nprints\n\nWord is cat, ends at position 3\nWord is dog, ends at position 7\nWord is house, ends at position 13\n\nA failed match or changing the target string resets the position.   If  you  don't  want  the\nposition reset after failure to match, add the \"/c\", as in \"/regex/gc\".\n\nIn  list  context,  \"/g\" returns a list of matched groupings, or if there are no groupings, a\nlist of matches to the whole regex.  So\n\n@words = ($x =~ /(\\w+)/g);  # matches,\n# $word[0] = 'cat'\n# $word[1] = 'dog'\n# $word[2] = 'house'\n"
                    },
                    {
                        "name": "Search and replace",
                        "content": "Search and replace is performed using \"s/regex/replacement/modifiers\".  The \"replacement\"  is\na Perl double-quoted string that replaces in the string whatever is matched with the \"regex\".\nThe  operator  \"=~\" is also used here to associate a string with \"s///\".  If matching against\n$, the \"$ =~\" can be  dropped.   If  there  is  a  match,  \"s///\"  returns  the  number  of\nsubstitutions made; otherwise it returns false.  Here are a few examples:\n\n$x = \"Time to feed the cat!\";\n$x =~ s/cat/hacker/;   # $x contains \"Time to feed the hacker!\"\n$y = \"'quoted words'\";\n$y =~ s/^'(.*)'$/$1/;  # strip single quotes,\n# $y contains \"quoted words\"\n\nWith  the  \"s///\" operator, the matched variables $1, $2, etc.  are immediately available for\nuse in the replacement expression. With the global modifier, \"s///g\" will search and  replace\nall occurrences of the regex in the string:\n\n$x = \"I batted 4 for 4\";\n$x =~ s/4/four/;   # $x contains \"I batted four for 4\"\n$x = \"I batted 4 for 4\";\n$x =~ s/4/four/g;  # $x contains \"I batted four for four\"\n\nThe  non-destructive  modifier  \"s///r\"  causes the result of the substitution to be returned\ninstead of modifying $ (or whatever variable the substitute was bound to with \"=~\"):\n\n$x = \"I like dogs.\";\n$y = $x =~ s/dogs/cats/r;\nprint \"$x $y\\n\"; # prints \"I like dogs. I like cats.\"\n\n$x = \"Cats are great.\";\nprint $x =~ s/Cats/Dogs/r =~ s/Dogs/Frogs/r =~\ns/Frogs/Hedgehogs/r, \"\\n\";\n# prints \"Hedgehogs are great.\"\n\n@foo = map { s/[a-z]/X/r } qw(a b c 1 2 3);\n# @foo is now qw(X X X 1 2 3)\n\nThe evaluation modifier \"s///e\" wraps an \"eval{...}\" around the replacement  string  and  the\nevaluated result is substituted for the matched substring.  Some examples:\n\n# reverse all the words in a string\n$x = \"the cat in the hat\";\n$x =~ s/(\\w+)/reverse $1/ge;   # $x contains \"eht tac ni eht tah\"\n\n# convert percentage to decimal\n$x = \"A 39% hit rate\";\n$x =~ s!(\\d+)%!$1/100!e;       # $x contains \"A 0.39 hit rate\"\n\nThe  last example shows that \"s///\" can use other delimiters, such as \"s!!!\" and \"s{}{}\", and\neven \"s{}//\".  If single quotes are used \"s'''\", then the regex and replacement  are  treated\nas single-quoted strings.\n"
                    },
                    {
                        "name": "The split operator",
                        "content": "\"split /regex/, string\" splits \"string\" into a list of substrings and returns that list.  The\nregex determines the character sequence that \"string\" is split with respect to.  For example,\nto split a string into words, use\n\n$x = \"Calvin and Hobbes\";\n@word = split /\\s+/, $x;  # $word[0] = 'Calvin'\n# $word[1] = 'and'\n# $word[2] = 'Hobbes'\n\nTo extract a comma-delimited list of numbers, use\n\n$x = \"1.618,2.718,   3.142\";\n@const = split /,\\s*/, $x;  # $const[0] = '1.618'\n# $const[1] = '2.718'\n# $const[2] = '3.142'\n\nIf  the  empty  regex  \"//\"  is used, the string is split into individual characters.  If the\nregex has groupings, then  the  list  produced  contains  the  matched  substrings  from  the\ngroupings as well:\n\n$x = \"/usr/bin\";\n@parts = split m!(/)!, $x;  # $parts[0] = ''\n# $parts[1] = '/'\n# $parts[2] = 'usr'\n# $parts[3] = '/'\n# $parts[4] = 'bin'\n\nSince the first character of $x matched the regex, \"split\" prepended an empty initial element\nto the list.\n"
                    },
                    {
                        "name": "\"use re 'strict'\"",
                        "content": "New  in  v5.22,  this applies stricter rules than otherwise when compiling regular expression\npatterns.  It can find things that, while legal, may not be what you intended.\n\nSee 'strict' in re.\n"
                    }
                ]
            },
            "BUGS": {
                "content": "None.\n",
                "subsections": []
            },
            "SEE ALSO": {
                "content": "This is just a quick start guide.  For a more in-depth tutorial on regexes, see perlretut and\nfor the reference page, see perlre.\n",
                "subsections": []
            },
            "AUTHOR AND COPYRIGHT": {
                "content": "Copyright (c) 2000 Mark Kvale All rights reserved.\n\nThis document may be distributed under the same terms as Perl itself.\n",
                "subsections": [
                    {
                        "name": "Acknowledgments",
                        "content": "The author would like to thank Mark-Jason Dominus, Tom Christiansen, Ilya  Zakharevich,  Brad\nHughes, and Mike Giroux for all their helpful comments.\n\nperl v5.38.2                                 2026-06-12                               PERLREQUICK(1)"
                    }
                ]
            }
        }
    }
}