404 regular-expression advice needed

It seems you can have both post ID’s and numbers in the page slug. So my-post-2 could be my-post with post ID 2 or my-post-2 with no post ID. In theory there is no way to solve this, but in practice maybe we can assume that you won’t have ten similar slugs, so the slug … Read more

preg_replace and comment_form_defaults

You can use regex anywhere you have a string to manipulate. That is basic PHP. There is nothing special about WordPress that changes that. But why use regex when there are other options? As much fun as it is, regex is tricky and easy to get wrong, and there is significant overhead to using it. … Read more

extract shortcodes from string

Where is this string located ? I know you wrote “without wordpress system” , but WordPress has a function get shortcode regex If you really want to avoid it for some reason, the following example might help you : // check the post for a short code function check_shortcode( $shortcode = NULL ) { $post_to_check … Read more

Wrap element with in any number of elements with PHP

Can I use a similar technique to ‘wrap’ my everything in the body with another div? Absolutely. Almost everything is possible. But you’re going to have to do something a little hackish. The the_content filter doesn’t actually filter the content of the entire page. That particular filter is used throughout WordPress to filter a variety … Read more

Regex difference: (\w+)? and (\w*)

(\w+)? and (\w*) both match the same (0..+inf word characters) However, there is a slight difference: In the first case, if this part of the regex matches “”, the capturing group is absent. In the second case, it is empty. In some languages, the former manifests as a null while the latter should always be “”. In Javascript, for example, In PHP … Read more

Regex to validate date formats dd/mm/YYYY, dd-mm-YYYY, dd.mm.YYYY, dd mmm YYYY, dd-mmm-YYYY, dd/mmm/YYYY, dd.mmm.YYYY with Leap Year Support

The regex you pasted does not validate leap years correctly, but there is one that does in the same post. I modified it to take dd/mm/yyyy, dd-mm-yyyy or dd.mm.yyyy. ^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$ I tested it a bit in the link Arun provided in his answer and also here and it seems to work. Edit February 14th 2019: I’

What is the best regular expression to check if a string is a valid URL?

I wrote my URL (actually IRI, internationalized) pattern to comply with RFC 3987 (http://www.faqs.org/rfcs/rfc3987.html). These are in PCRE syntax. For absolute IRIs (internationalized): To also allow relative IRIs: How they were compiled (in PHP): Edit 7 March 2011: Because of the way PHP handles backslashes in quoted strings, these are unusable by default. You’ll need … Read more

How to replace a string in an existing file in Perl

Use a one-liner: Explanation -p processes, then prints <> line by line -i activates in-place editing. Files are backed up using the .bak extension The regex substitution acts on the implicit variable, which are the contents of the file, line-by-line