Regular expression - starting and ending with a character string
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 string
add a comment |
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 string
1
There ia a good tutorial here: regular-expressions.info/tutorial.html
– Casimir et Hippolyte
Aug 2 '13 at 19:02
Why regex? Wouldn't you rather use globbing when looking for files?
– Tim Pietzcker
Aug 2 '13 at 19:04
How would you do it using globbing?
– Ken Shoufer
Aug 2 '13 at 20:07
add a comment |
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 string
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 string
regex string
asked Aug 2 '13 at 19:01
Ken ShouferKen Shoufer
4223613
4223613
1
There ia a good tutorial here: regular-expressions.info/tutorial.html
– Casimir et Hippolyte
Aug 2 '13 at 19:02
Why regex? Wouldn't you rather use globbing when looking for files?
– Tim Pietzcker
Aug 2 '13 at 19:04
How would you do it using globbing?
– Ken Shoufer
Aug 2 '13 at 20:07
add a comment |
1
There ia a good tutorial here: regular-expressions.info/tutorial.html
– Casimir et Hippolyte
Aug 2 '13 at 19:02
Why regex? Wouldn't you rather use globbing when looking for files?
– Tim Pietzcker
Aug 2 '13 at 19:04
How would you do it using globbing?
– Ken Shoufer
Aug 2 '13 at 20:07
1
1
There ia a good tutorial here: regular-expressions.info/tutorial.html
– Casimir et Hippolyte
Aug 2 '13 at 19:02
There ia a good tutorial here: regular-expressions.info/tutorial.html
– Casimir et Hippolyte
Aug 2 '13 at 19:02
Why regex? Wouldn't you rather use globbing when looking for files?
– Tim Pietzcker
Aug 2 '13 at 19:04
Why regex? Wouldn't you rather use globbing when looking for files?
– Tim Pietzcker
Aug 2 '13 at 19:04
How would you do it using globbing?
– Ken Shoufer
Aug 2 '13 at 20:07
How would you do it using globbing?
– Ken Shoufer
Aug 2 '13 at 20:07
add a comment |
3 Answers
3
active
oldest
votes
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
add a comment |
^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.
This should also work: ^wp.+.php$ The only difference is the '+', which makes the dot "greedy".
– Adam Howell
Apr 8 '16 at 3:35
4
@AdamHowell+
and*
are both greedy, although greediness doesn't make a difference in this case. The difference is that*
matches zero or more characters, and the+
matches one or more characters. So if someone wanted to match "wp.php", they should use*
; if they specifically wanted to not match that, they should use+
.
– Michelle
Apr 11 '16 at 10:34
add a comment |
Example:
ajshdjashdjashdlasdlhdlSTARTasdasdsdaasdENDaknsdklansdlknaldknaaklsdn
1) STARTw*END
return: STARTasdasdsdaasdEND - will give you words between START and END
2) STARTd*END
return: START12121212END - will give you numbers between START and END
3) STARTd*_d*END
return: START1212_1212END - will give you numbers between START and END having _
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18024298%2fregular-expression-starting-and-ending-with-a-character-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
answered Aug 2 '13 at 19:04
SyonSyon
5,73643037
5,73643037
add a comment |
add a comment |
^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.
This should also work: ^wp.+.php$ The only difference is the '+', which makes the dot "greedy".
– Adam Howell
Apr 8 '16 at 3:35
4
@AdamHowell+
and*
are both greedy, although greediness doesn't make a difference in this case. The difference is that*
matches zero or more characters, and the+
matches one or more characters. So if someone wanted to match "wp.php", they should use*
; if they specifically wanted to not match that, they should use+
.
– Michelle
Apr 11 '16 at 10:34
add a comment |
^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.
This should also work: ^wp.+.php$ The only difference is the '+', which makes the dot "greedy".
– Adam Howell
Apr 8 '16 at 3:35
4
@AdamHowell+
and*
are both greedy, although greediness doesn't make a difference in this case. The difference is that*
matches zero or more characters, and the+
matches one or more characters. So if someone wanted to match "wp.php", they should use*
; if they specifically wanted to not match that, they should use+
.
– Michelle
Apr 11 '16 at 10:34
add a comment |
^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.
^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.
answered Aug 2 '13 at 19:05
MichelleMichelle
2,3442030
2,3442030
This should also work: ^wp.+.php$ The only difference is the '+', which makes the dot "greedy".
– Adam Howell
Apr 8 '16 at 3:35
4
@AdamHowell+
and*
are both greedy, although greediness doesn't make a difference in this case. The difference is that*
matches zero or more characters, and the+
matches one or more characters. So if someone wanted to match "wp.php", they should use*
; if they specifically wanted to not match that, they should use+
.
– Michelle
Apr 11 '16 at 10:34
add a comment |
This should also work: ^wp.+.php$ The only difference is the '+', which makes the dot "greedy".
– Adam Howell
Apr 8 '16 at 3:35
4
@AdamHowell+
and*
are both greedy, although greediness doesn't make a difference in this case. The difference is that*
matches zero or more characters, and the+
matches one or more characters. So if someone wanted to match "wp.php", they should use*
; if they specifically wanted to not match that, they should use+
.
– Michelle
Apr 11 '16 at 10:34
This should also work: ^wp.+.php$ The only difference is the '+', which makes the dot "greedy".
– Adam Howell
Apr 8 '16 at 3:35
This should also work: ^wp.+.php$ The only difference is the '+', which makes the dot "greedy".
– Adam Howell
Apr 8 '16 at 3:35
4
4
@AdamHowell
+
and *
are both greedy, although greediness doesn't make a difference in this case. The difference is that *
matches zero or more characters, and the +
matches one or more characters. So if someone wanted to match "wp.php", they should use *
; if they specifically wanted to not match that, they should use +
.– Michelle
Apr 11 '16 at 10:34
@AdamHowell
+
and *
are both greedy, although greediness doesn't make a difference in this case. The difference is that *
matches zero or more characters, and the +
matches one or more characters. So if someone wanted to match "wp.php", they should use *
; if they specifically wanted to not match that, they should use +
.– Michelle
Apr 11 '16 at 10:34
add a comment |
Example:
ajshdjashdjashdlasdlhdlSTARTasdasdsdaasdENDaknsdklansdlknaldknaaklsdn
1) STARTw*END
return: STARTasdasdsdaasdEND - will give you words between START and END
2) STARTd*END
return: START12121212END - will give you numbers between START and END
3) STARTd*_d*END
return: START1212_1212END - will give you numbers between START and END having _
add a comment |
Example:
ajshdjashdjashdlasdlhdlSTARTasdasdsdaasdENDaknsdklansdlknaldknaaklsdn
1) STARTw*END
return: STARTasdasdsdaasdEND - will give you words between START and END
2) STARTd*END
return: START12121212END - will give you numbers between START and END
3) STARTd*_d*END
return: START1212_1212END - will give you numbers between START and END having _
add a comment |
Example:
ajshdjashdjashdlasdlhdlSTARTasdasdsdaasdENDaknsdklansdlknaldknaaklsdn
1) STARTw*END
return: STARTasdasdsdaasdEND - will give you words between START and END
2) STARTd*END
return: START12121212END - will give you numbers between START and END
3) STARTd*_d*END
return: START1212_1212END - will give you numbers between START and END having _
Example:
ajshdjashdjashdlasdlhdlSTARTasdasdsdaasdENDaknsdklansdlknaldknaaklsdn
1) STARTw*END
return: STARTasdasdsdaasdEND - will give you words between START and END
2) STARTd*END
return: START12121212END - will give you numbers between START and END
3) STARTd*_d*END
return: START1212_1212END - will give you numbers between START and END having _
answered Dec 31 '16 at 11:37
Nayan HodarNayan Hodar
1896
1896
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18024298%2fregular-expression-starting-and-ending-with-a-character-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
There ia a good tutorial here: regular-expressions.info/tutorial.html
– Casimir et Hippolyte
Aug 2 '13 at 19:02
Why regex? Wouldn't you rather use globbing when looking for files?
– Tim Pietzcker
Aug 2 '13 at 19:04
How would you do it using globbing?
– Ken Shoufer
Aug 2 '13 at 20:07