{"id":30101,"date":"2024-11-12T12:39:25","date_gmt":"2024-11-12T12:39:25","guid":{"rendered":"https:\/\/www.seobility.net\/en\/blog\/wiki\/regex\/"},"modified":"2025-02-26T13:05:20","modified_gmt":"2025-02-26T13:05:20","slug":"RegEx","status":"publish","type":"wiki","link":"https:\/\/www.seobility.net\/en\/wiki\/RegEx","title":{"rendered":"RegEx"},"content":{"rendered":"<h2><span class=\"mw-headline\" id=\"Definition\">Definition<\/span><\/h2>\n<p>RegEx stands for &#8216;regular expression&#8217; and is a method used by programmers to define search patterns. Regex is useful for extracting information from large blocks of data. Data can take many forms, whether that be plain text, files, or code. A regex search pattern is much more powerful and flexible than simple string searches, such as the search queries typically used with search engines.\n<\/p>\n<p>For example, a regular expression is used when a password policy is stored in software that specifies certain character combinations for passwords. For such a password rule, the expression could look as follows:\n<\/p>\n<pre>(?=^.{8,}$)((?=.*\\d)|(?=.*\\W+))(?![.\\n])(?=.*[A-Z])(?=.*[a-z]).*$\"<\/pre>\n<p>This rule contains numerous specifications, such as the minimum length of 8 characters and the use of upper and lower case letters. For example, the expression .{8,} means that any character (symbolized by the dot) should occur eight times or more ({8,}).\n<\/p>\n<h2><span class=\"mw-headline\" id=\"Components_of_regular_expressions\">Components of regular expressions<\/span><\/h2>\n<p>Regular expressions are commonly found in many different programming languages, but their exact implementation can differ. This means that occasionally, some characters may be used in different ways in different implementations. However, sometimes a character has a relatively universal use. Below are some common regular expression components.\n<\/p>\n<h3><span class=\"mw-headline\" id=\"Anchors\">Anchors<\/span><\/h3>\n<p>Anchors are characters that specify the location within a particular string to search. Regex was developed originally for line-based systems, so a lot of regex was developed around searching within lines. To find a character &#8220;A&#8221; in a string, you can use characters from the following list to find a match within a line:\n<\/p>\n<ul>\n<li> ^A &#8211; Match at the beginning of a line.<\/li>\n<li> A$ &#8211; Match at the end of a line.<\/li>\n<\/ul>\n<h3><span class=\"mw-headline\" id=\"Character_sets\">Character sets<\/span><\/h3>\n<p>Character sets allow you to define explicit parameters for the type of text to be searched for. As an example, numerical ranges can be searched for, using [0-9]. However, regex supports character matching, so it can be useful for finding letter ranges, or for supporting alternate spellings. For example, gr[ae]y will match both &#8216;gray&#8217; and &#8216;grey&#8217;.\n<\/p>\n<ul>\n<li> [0-9] &#8211; Match a range of numbers from 0-9.<\/li>\n<li> [a-z] &#8211; Match lowercase letters from a-z.<\/li>\n<li> [A-Z] &#8211; Match uppercase letters from A-Z.<\/li>\n<li> [.] &#8211; Match any character except line break characters.<\/li>\n<\/ul>\n<h3><span class=\"mw-headline\" id=\"Modifiers\">Modifiers<\/span><\/h3>\n<p>Modifiers can be used to alter the behavior of regex strings. They are typically wrapped in brackets and start with a question mark. Many modifiers are implementation-dependent, but below are some example characters.\n<\/p>\n<ul>\n<li> (?c) &#8211; Turns off case sensitivity.<\/li>\n<li> (?s) &#8211; Make the dot character include matches for line break characters.<\/li>\n<\/ul>\n<h2><span class=\"mw-headline\" id=\"Chaining_regular_expressions\">Chaining regular expressions<\/span><\/h2>\n<p>Regular expressions can be chained together using the pipe character (|). This allows for multiple search options to be acceptable in a single regex string. For example, the regex string &#8216;(string1|string2|string3)&#8217; will search for &#8216;string1&#8217;, &#8216;string2&#8217;, and &#8216;string3&#8217; within the same query, rather than having to run 3 separate queries. These can be chained together with any other regex character and with virtually no limitations as to how many.\n<\/p>\n<h2><span class=\"mw-headline\" id=\"Quantifiers_in_regular_expressions\">Quantifiers in regular expressions<\/span><\/h2>\n<p>Quantifiers allow you to specify how many times you want a particular regex string to match. Quantifiers usually come in two variants: lazy and greedy. By default, regex matching is eager and will match as much as possible, which is not always the desired behavior. Lazy quantifiers allow you to limit how much is matched, and you can further specify how many times matches are found with other limiting characters, such as:\n<\/p>\n<ul>\n<li> * &#8211; Match 0 or more times.<\/li>\n<li> + &#8211; Match 1 or more times<\/li>\n<li> { n } &#8211; Match exactly n times.<\/li>\n<\/ul>\n<h2><span class=\"mw-headline\" id=\"Advanced_regular_expressions\">Advanced regular expressions<\/span><\/h2>\n<p>Regular expressions support advanced concepts, such as recursion, backreferencing, grouping, subroutines, conditionals, and more. These features allow you to find very specific information within large data sets, and you can even create regex strings to find results within results.\n<\/p>\n<h2><span class=\"mw-headline\" id=\"Example_of_a_regular_expression_in_web_server_administration\">Example of a regular expression in web server administration<\/span><\/h2>\n<p>Regex can be very useful for web server administrators as a tool to facilitate routing and searching for information. <a href=\"\/en\/wiki\/Log_File\" title=\"Log File\">Log files<\/a> follow typical patterns, so regex can be an easy way to help find specific messages in files that can be very large, like access logs.\n<\/p>\n<p>Regex is also used by server software, such as Apache. Apace uses <a href=\"\/en\/wiki\/htaccess\" title=\".htaccess\">.htaccess<\/a> files for rewrite rules and rewrite conditions, that are used to dictate how a server should respond to requests. Regex can be used in .htaccess files to interpret incoming <a href=\"\/en\/wiki\/URL\" title=\"URL\">URL<\/a> access requests and re-route or reject them as needed.\n<\/p>\n<p>For example, a typical line in a .htaccess file that uses regex to aid in routing requests looks like this:\n<\/p>\n<pre>RewriteRule ^index\\.php$ - [L]<\/pre>\n<p>This uses regex to match any server request for a URL containing index.php, regardless of what comes after. Without <a href=\"\/en\/wiki\/Mod_Rewrite\" title=\"Mod Rewrite\">mod_rewrite<\/a>, a typical URL might look like: <code>www.example.com\/index.php?p=123<\/code>, but using regex and mod_rewrite, instead, the same page can be accessed using a URL more like: <code>www.example.com\/my-blog-post<\/code>\n<\/p>\n<h2><span class=\"mw-headline\" id=\"Importance_for_SEO\">Importance for SEO<\/span><\/h2>\n<p>URLs that contain a page&#8217;s main <a href=\"\/en\/wiki\/Keyword\" title=\"Keyword\">keywords<\/a> are very beneficial for SEO. Search engines, like Google, consider keywords in URLs when ranking pages. Using keywords in URLs helps both search engines and regular users understand what sort of content to expect when they access a page.\n<\/p>\n<p>Regex is used in .htaccess files to help achieve this. Popular <a href=\"\/en\/wiki\/content-management-system-cms\" title=\"Content Management System (CMS)\">CMS<\/a>, such as WordPress, use this approach, which lets admins create and edit custom URL slugs for pages, without having to edit any files. For example, when searching for results on an e-commerce site, behind the scenes the parameters of a search query might be included in the URL, generating a URL such as: <code>www.example.com\/results.php?category=461&amp;color=green&amp;size=l<\/code>\n<\/p>\n<p>.htaccess uses regex to identify the individual components of a URL string, such as <code>color=green<\/code> and <code>category=461<\/code>, and replace them with a much better URL for users and search engines, like: <code>www.example.com\/search-results\/toys\/green\/large<\/code>. Behind the scenes, everything will work the same, but this URL is much better for SEO; the content of the page is clearer and it is a much cleaner URL.\n<\/p>\n<h2><span class=\"mw-headline\" id=\"Related_links\">Related links<\/span><\/h2>\n<ul>\n<li> <a rel=\"nofollow\" class=\"external free\" href=\"https:\/\/support.google.com\/analytics\/answer\/1034324?hl=en\">https:\/\/support.google.com\/analytics\/answer\/1034324?hl=en<\/a><\/li>\n<li> <a rel=\"nofollow\" class=\"external free\" href=\"https:\/\/www.lovesdata.com\/blog\/regex-google-analytics\">https:\/\/www.lovesdata.com\/blog\/regex-google-analytics<\/a><\/li>\n<\/ul>\n<h2><span class=\"mw-headline\" id=\"Similar_articles\">Similar articles<\/span><\/h2>\n<ul>\n<li> <a href=\"\/en\/wiki\/Mod_Rewrite\" title=\"Mod Rewrite\">Mod Rewrite<\/a><\/li>\n<li> <a href=\"\/en\/wiki\/SEO-friendly_URLs\" title=\"SEO-friendly URLs\">SEO-friendly URLs<\/a><\/li>\n<\/ul>\n<p><script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"Article\",\n  \"author\": {\n    \"@type\": \"Organization\",\n    \"name\": \"Seobility\",\n    \"url\": \"https:\/\/www.seobility.net\/\"\n  }\n}\n<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Definition RegEx stands for &#8216;regular expression&#8217; and is a method used by programmers to define search patterns. Regex is useful for extracting information from large blocks of data. Data can take many forms, whether that be plain text, files, or code. A regex search pattern is much more powerful and flexible than simple string searches, [&hellip;]<\/p>\n","protected":false},"author":38,"featured_media":0,"comment_status":"open","ping_status":"open","template":"","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"class_list":["post-30101","wiki","type-wiki","status-publish","hentry","wiki_categories-web-development"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>RegEx Explained + When and how to use it - Seobility Wiki<\/title>\n<meta name=\"description\" content=\"RegEx stands for regular expression and is a method used to define search patterns that are way more powerful and flexible than simple string searches.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.seobility.net\/en\/wiki\/RegEx\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"RegEx Explained + When and how to use it - Seobility Wiki\" \/>\n<meta property=\"og:description\" content=\"RegEx stands for regular expression and is a method used to define search patterns that are way more powerful and flexible than simple string searches.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.seobility.net\/en\/wiki\/RegEx\" \/>\n<meta property=\"og:site_name\" content=\"Seobility\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/seobility.net\/\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-26T13:05:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.seobility.net\/wp-content\/uploads\/2025\/06\/seobility-og-image.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@seobility_net\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"ScholarlyArticle\",\"@id\":\"https:\\\/\\\/www.seobility.net\\\/en\\\/wiki\\\/RegEx#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.seobility.net\\\/en\\\/wiki\\\/RegEx\"},\"author\":{\"name\":\"Seobility Wiki Team\",\"@id\":\"https:\\\/\\\/www.seobility.net\\\/en\\\/#\\\/schema\\\/person\\\/c838e66a3b46262b4c963df5cd5b2d8a\"},\"headline\":\"RegEx\",\"datePublished\":\"2024-11-12T12:39:25+00:00\",\"dateModified\":\"2025-02-26T13:05:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.seobility.net\\\/en\\\/wiki\\\/RegEx\"},\"wordCount\":980,\"commentCount\":0,\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.seobility.net\\\/en\\\/wiki\\\/RegEx#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.seobility.net\\\/en\\\/wiki\\\/RegEx\",\"url\":\"https:\\\/\\\/www.seobility.net\\\/en\\\/wiki\\\/RegEx\",\"name\":\"RegEx Explained + When and how to use it - Seobility Wiki\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.seobility.net\\\/en\\\/#website\"},\"datePublished\":\"2024-11-12T12:39:25+00:00\",\"dateModified\":\"2025-02-26T13:05:20+00:00\",\"description\":\"RegEx stands for regular expression and is a method used to define search patterns that are way more powerful and flexible than simple string searches.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.seobility.net\\\/en\\\/wiki\\\/RegEx#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.seobility.net\\\/en\\\/wiki\\\/RegEx\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.seobility.net\\\/en\\\/wiki\\\/RegEx#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.seobility.net\\\/en\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Wiki\",\"item\":\"https:\\\/\\\/www.seobility.net\\\/en\\\/wiki\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"RegEx\",\"item\":\"https:\\\/\\\/www.seobility.net\\\/en\\\/wp-json\\\/wp\\\/v2\\\/wiki\\\/30101\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.seobility.net\\\/en\\\/#website\",\"url\":\"https:\\\/\\\/www.seobility.net\\\/en\\\/\",\"name\":\"Seobility\",\"description\":\"Online SEO Software &amp; Tools For Better Rankings\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.seobility.net\\\/en\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.seobility.net\\\/en\\\/#\\\/schema\\\/person\\\/c838e66a3b46262b4c963df5cd5b2d8a\",\"name\":\"Seobility Wiki Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.seobility.net\\\/wp-content\\\/uploads\\\/2025\\\/06\\\/Seobility-S-2025-150x150.png\",\"url\":\"https:\\\/\\\/www.seobility.net\\\/wp-content\\\/uploads\\\/2025\\\/06\\\/Seobility-S-2025-150x150.png\",\"contentUrl\":\"https:\\\/\\\/www.seobility.net\\\/wp-content\\\/uploads\\\/2025\\\/06\\\/Seobility-S-2025-150x150.png\",\"caption\":\"Seobility Wiki Team\"},\"description\":\"The Seobility Wiki team consists of seasoned SEOs, digital marketing professionals, and business experts with combined hands-on experience in SEO, online marketing and web development. All our articles went through a multi-level editorial process to provide you with the best possible quality and truly helpful information. Learn more about the people behind the Seobility Wiki.\",\"url\":\"https:\\\/\\\/www.seobility.net\\\/en\\\/blog\\\/author\\\/seobility-wiki-team\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"RegEx Explained + When and how to use it - Seobility Wiki","description":"RegEx stands for regular expression and is a method used to define search patterns that are way more powerful and flexible than simple string searches.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.seobility.net\/en\/wiki\/RegEx","og_locale":"en_US","og_type":"article","og_title":"RegEx Explained + When and how to use it - Seobility Wiki","og_description":"RegEx stands for regular expression and is a method used to define search patterns that are way more powerful and flexible than simple string searches.","og_url":"https:\/\/www.seobility.net\/en\/wiki\/RegEx","og_site_name":"Seobility","article_publisher":"https:\/\/www.facebook.com\/seobility.net\/","article_modified_time":"2025-02-26T13:05:20+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/www.seobility.net\/wp-content\/uploads\/2025\/06\/seobility-og-image.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_site":"@seobility_net","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"ScholarlyArticle","@id":"https:\/\/www.seobility.net\/en\/wiki\/RegEx#article","isPartOf":{"@id":"https:\/\/www.seobility.net\/en\/wiki\/RegEx"},"author":{"name":"Seobility Wiki Team","@id":"https:\/\/www.seobility.net\/en\/#\/schema\/person\/c838e66a3b46262b4c963df5cd5b2d8a"},"headline":"RegEx","datePublished":"2024-11-12T12:39:25+00:00","dateModified":"2025-02-26T13:05:20+00:00","mainEntityOfPage":{"@id":"https:\/\/www.seobility.net\/en\/wiki\/RegEx"},"wordCount":980,"commentCount":0,"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.seobility.net\/en\/wiki\/RegEx#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.seobility.net\/en\/wiki\/RegEx","url":"https:\/\/www.seobility.net\/en\/wiki\/RegEx","name":"RegEx Explained + When and how to use it - Seobility Wiki","isPartOf":{"@id":"https:\/\/www.seobility.net\/en\/#website"},"datePublished":"2024-11-12T12:39:25+00:00","dateModified":"2025-02-26T13:05:20+00:00","description":"RegEx stands for regular expression and is a method used to define search patterns that are way more powerful and flexible than simple string searches.","breadcrumb":{"@id":"https:\/\/www.seobility.net\/en\/wiki\/RegEx#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.seobility.net\/en\/wiki\/RegEx"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.seobility.net\/en\/wiki\/RegEx#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.seobility.net\/en\/"},{"@type":"ListItem","position":2,"name":"Wiki","item":"https:\/\/www.seobility.net\/en\/wiki"},{"@type":"ListItem","position":3,"name":"RegEx","item":"https:\/\/www.seobility.net\/en\/wp-json\/wp\/v2\/wiki\/30101"}]},{"@type":"WebSite","@id":"https:\/\/www.seobility.net\/en\/#website","url":"https:\/\/www.seobility.net\/en\/","name":"Seobility","description":"Online SEO Software &amp; Tools For Better Rankings","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.seobility.net\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.seobility.net\/en\/#\/schema\/person\/c838e66a3b46262b4c963df5cd5b2d8a","name":"Seobility Wiki Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.seobility.net\/wp-content\/uploads\/2025\/06\/Seobility-S-2025-150x150.png","url":"https:\/\/www.seobility.net\/wp-content\/uploads\/2025\/06\/Seobility-S-2025-150x150.png","contentUrl":"https:\/\/www.seobility.net\/wp-content\/uploads\/2025\/06\/Seobility-S-2025-150x150.png","caption":"Seobility Wiki Team"},"description":"The Seobility Wiki team consists of seasoned SEOs, digital marketing professionals, and business experts with combined hands-on experience in SEO, online marketing and web development. All our articles went through a multi-level editorial process to provide you with the best possible quality and truly helpful information. Learn more about the people behind the Seobility Wiki.","url":"https:\/\/www.seobility.net\/en\/blog\/author\/seobility-wiki-team\/"}]}},"_links":{"self":[{"href":"https:\/\/www.seobility.net\/en\/wp-json\/wp\/v2\/wiki\/30101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.seobility.net\/en\/wp-json\/wp\/v2\/wiki"}],"about":[{"href":"https:\/\/www.seobility.net\/en\/wp-json\/wp\/v2\/types\/wiki"}],"author":[{"embeddable":true,"href":"https:\/\/www.seobility.net\/en\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/www.seobility.net\/en\/wp-json\/wp\/v2\/comments?post=30101"}],"version-history":[{"count":0,"href":"https:\/\/www.seobility.net\/en\/wp-json\/wp\/v2\/wiki\/30101\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.seobility.net\/en\/wp-json\/wp\/v2\/media?parent=30101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}