Regular expression - starting and ending with a character string

RegexString

Regex Problem Overview


I would like to write a regular expression that starts with the string "wp" and ends with the string "php" to locate a file in a directory. How do I do it?

Example file: wp-comments-post.php

Regex Solutions


Solution 1 - Regex

This should do it for you ^wp.*php$

Matches

wp-comments-post.php
wp.something.php
wp.php

Doesn't match

something-wp.php
wp.php.txt

Solution 2 - Regex

^wp.*\.php$ Should do the trick.

The .* means "any character, repeated 0 or more times". The next . is escaped because it's a special character, and you want a literal period (".php"). Don't forget that if you're typing this in as a literal string in something like C#, Java, etc., you need to escape the backslash because it's a special character in many literal strings.

Solution 3 - Regex

Example: ajshdjashdjashdlasdlhdlSTARTasdasdsdaasdENDaknsdklansdlknaldknaaklsdn

  1. START\w*END return: STARTasdasdsdaasdEND - will give you words between START and END

  2. START\d*END return: START12121212END - will give you numbers between START and END

  3. START\d*_\d*END return: START1212_1212END - will give you numbers between START and END having _

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKen ShouferView Question on Stackoverflow
Solution 1 - RegexSyonView Answer on Stackoverflow
Solution 2 - RegexMichelleView Answer on Stackoverflow
Solution 3 - RegexNayan HodarView Answer on Stackoverflow