Why regular expression '[a-z][0-9]+$' not work
up vote
0
down vote
favorite
I use the regular expression to handle the string "abc123".
The command below is work and return value "c123"
echo abc123 | grep -o [a-z][0-9]*$
But the command below does not work.
echo abc123 | grep -o [a-z][0-9]+$
Why do I get this result?
I knew the '*' is used to matches the preceding pattern element zero or more time, and '+' is used to matches the preceding pattern element at least one or more time.
So this situation makes me confused.
regular-expression
New contributor
add a comment |
up vote
0
down vote
favorite
I use the regular expression to handle the string "abc123".
The command below is work and return value "c123"
echo abc123 | grep -o [a-z][0-9]*$
But the command below does not work.
echo abc123 | grep -o [a-z][0-9]+$
Why do I get this result?
I knew the '*' is used to matches the preceding pattern element zero or more time, and '+' is used to matches the preceding pattern element at least one or more time.
So this situation makes me confused.
regular-expression
New contributor
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I use the regular expression to handle the string "abc123".
The command below is work and return value "c123"
echo abc123 | grep -o [a-z][0-9]*$
But the command below does not work.
echo abc123 | grep -o [a-z][0-9]+$
Why do I get this result?
I knew the '*' is used to matches the preceding pattern element zero or more time, and '+' is used to matches the preceding pattern element at least one or more time.
So this situation makes me confused.
regular-expression
New contributor
I use the regular expression to handle the string "abc123".
The command below is work and return value "c123"
echo abc123 | grep -o [a-z][0-9]*$
But the command below does not work.
echo abc123 | grep -o [a-z][0-9]+$
Why do I get this result?
I knew the '*' is used to matches the preceding pattern element zero or more time, and '+' is used to matches the preceding pattern element at least one or more time.
So this situation makes me confused.
regular-expression
regular-expression
New contributor
New contributor
edited 2 days ago
Rui F Ribeiro
38.5k1479128
38.5k1479128
New contributor
asked 2 days ago
IsaraSu
31
31
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
+
is only a quantifier in extended regular expressions (ERE):
$ echo abc123 | grep -Eo '[a-z][0-9]+$'
c123
In basic regular expressions (BRE) it matches literal +
, although you can use {1,}
instead, or in GNU grep (-o
is already a GNU extension anyway), +
:
$ echo abc123 | grep -o '[a-z][0-9]+$'
c123
(note the quotes to prevent [
and from being interpreted by the shell).
Just a comment on @StéphaneChazelas' edit:-o
is a GNU extension, but it's also available in (some) othergrep
implementations. However,+
in a basic regular expression is GNU-only as far as I know.
– Kusalananda
yesterday
@Kusalananda, I agree it's a bit of a shortcut. Several other implementations have picked some of the GNU extensions over the years.+
is also found in some non-GNU implementations like busybox or ast-open. Also note that many BSDs still use a fork of an old GNUgrep
, so will support old GNU extensions like those.
– Stéphane Chazelas
yesterday
add a comment |
up vote
0
down vote
+
in grep need to be escaped to take effect. Instead of
echo abc123 | grep -o [a-z][0-9]+$
You need to write
echo abc123 | grep -o '[a-z][0-9]+$'
There are other characters that need to be escaped as well. It is also good practice to put your regex in single quotes.
You can also use egrep
which is a synonym of grep -E
and uses Extended RE, as commented by @muru.
Or use ERE with-E
– muru
2 days ago
The escaped+
in basic regular expressions is a non-standard GNU extension.
– Kusalananda
2 days ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});
}
});
IsaraSu is a new contributor. Be nice, and check out our Code of Conduct.
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%2funix.stackexchange.com%2fquestions%2f487484%2fwhy-regular-expression-a-z0-9-not-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
+
is only a quantifier in extended regular expressions (ERE):
$ echo abc123 | grep -Eo '[a-z][0-9]+$'
c123
In basic regular expressions (BRE) it matches literal +
, although you can use {1,}
instead, or in GNU grep (-o
is already a GNU extension anyway), +
:
$ echo abc123 | grep -o '[a-z][0-9]+$'
c123
(note the quotes to prevent [
and from being interpreted by the shell).
Just a comment on @StéphaneChazelas' edit:-o
is a GNU extension, but it's also available in (some) othergrep
implementations. However,+
in a basic regular expression is GNU-only as far as I know.
– Kusalananda
yesterday
@Kusalananda, I agree it's a bit of a shortcut. Several other implementations have picked some of the GNU extensions over the years.+
is also found in some non-GNU implementations like busybox or ast-open. Also note that many BSDs still use a fork of an old GNUgrep
, so will support old GNU extensions like those.
– Stéphane Chazelas
yesterday
add a comment |
up vote
4
down vote
accepted
+
is only a quantifier in extended regular expressions (ERE):
$ echo abc123 | grep -Eo '[a-z][0-9]+$'
c123
In basic regular expressions (BRE) it matches literal +
, although you can use {1,}
instead, or in GNU grep (-o
is already a GNU extension anyway), +
:
$ echo abc123 | grep -o '[a-z][0-9]+$'
c123
(note the quotes to prevent [
and from being interpreted by the shell).
Just a comment on @StéphaneChazelas' edit:-o
is a GNU extension, but it's also available in (some) othergrep
implementations. However,+
in a basic regular expression is GNU-only as far as I know.
– Kusalananda
yesterday
@Kusalananda, I agree it's a bit of a shortcut. Several other implementations have picked some of the GNU extensions over the years.+
is also found in some non-GNU implementations like busybox or ast-open. Also note that many BSDs still use a fork of an old GNUgrep
, so will support old GNU extensions like those.
– Stéphane Chazelas
yesterday
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
+
is only a quantifier in extended regular expressions (ERE):
$ echo abc123 | grep -Eo '[a-z][0-9]+$'
c123
In basic regular expressions (BRE) it matches literal +
, although you can use {1,}
instead, or in GNU grep (-o
is already a GNU extension anyway), +
:
$ echo abc123 | grep -o '[a-z][0-9]+$'
c123
(note the quotes to prevent [
and from being interpreted by the shell).
+
is only a quantifier in extended regular expressions (ERE):
$ echo abc123 | grep -Eo '[a-z][0-9]+$'
c123
In basic regular expressions (BRE) it matches literal +
, although you can use {1,}
instead, or in GNU grep (-o
is already a GNU extension anyway), +
:
$ echo abc123 | grep -o '[a-z][0-9]+$'
c123
(note the quotes to prevent [
and from being interpreted by the shell).
edited yesterday
Stéphane Chazelas
297k54562907
297k54562907
answered 2 days ago
steeldriver
34.1k34983
34.1k34983
Just a comment on @StéphaneChazelas' edit:-o
is a GNU extension, but it's also available in (some) othergrep
implementations. However,+
in a basic regular expression is GNU-only as far as I know.
– Kusalananda
yesterday
@Kusalananda, I agree it's a bit of a shortcut. Several other implementations have picked some of the GNU extensions over the years.+
is also found in some non-GNU implementations like busybox or ast-open. Also note that many BSDs still use a fork of an old GNUgrep
, so will support old GNU extensions like those.
– Stéphane Chazelas
yesterday
add a comment |
Just a comment on @StéphaneChazelas' edit:-o
is a GNU extension, but it's also available in (some) othergrep
implementations. However,+
in a basic regular expression is GNU-only as far as I know.
– Kusalananda
yesterday
@Kusalananda, I agree it's a bit of a shortcut. Several other implementations have picked some of the GNU extensions over the years.+
is also found in some non-GNU implementations like busybox or ast-open. Also note that many BSDs still use a fork of an old GNUgrep
, so will support old GNU extensions like those.
– Stéphane Chazelas
yesterday
Just a comment on @StéphaneChazelas' edit:
-o
is a GNU extension, but it's also available in (some) other grep
implementations. However, +
in a basic regular expression is GNU-only as far as I know.– Kusalananda
yesterday
Just a comment on @StéphaneChazelas' edit:
-o
is a GNU extension, but it's also available in (some) other grep
implementations. However, +
in a basic regular expression is GNU-only as far as I know.– Kusalananda
yesterday
@Kusalananda, I agree it's a bit of a shortcut. Several other implementations have picked some of the GNU extensions over the years.
+
is also found in some non-GNU implementations like busybox or ast-open. Also note that many BSDs still use a fork of an old GNU grep
, so will support old GNU extensions like those.– Stéphane Chazelas
yesterday
@Kusalananda, I agree it's a bit of a shortcut. Several other implementations have picked some of the GNU extensions over the years.
+
is also found in some non-GNU implementations like busybox or ast-open. Also note that many BSDs still use a fork of an old GNU grep
, so will support old GNU extensions like those.– Stéphane Chazelas
yesterday
add a comment |
up vote
0
down vote
+
in grep need to be escaped to take effect. Instead of
echo abc123 | grep -o [a-z][0-9]+$
You need to write
echo abc123 | grep -o '[a-z][0-9]+$'
There are other characters that need to be escaped as well. It is also good practice to put your regex in single quotes.
You can also use egrep
which is a synonym of grep -E
and uses Extended RE, as commented by @muru.
Or use ERE with-E
– muru
2 days ago
The escaped+
in basic regular expressions is a non-standard GNU extension.
– Kusalananda
2 days ago
add a comment |
up vote
0
down vote
+
in grep need to be escaped to take effect. Instead of
echo abc123 | grep -o [a-z][0-9]+$
You need to write
echo abc123 | grep -o '[a-z][0-9]+$'
There are other characters that need to be escaped as well. It is also good practice to put your regex in single quotes.
You can also use egrep
which is a synonym of grep -E
and uses Extended RE, as commented by @muru.
Or use ERE with-E
– muru
2 days ago
The escaped+
in basic regular expressions is a non-standard GNU extension.
– Kusalananda
2 days ago
add a comment |
up vote
0
down vote
up vote
0
down vote
+
in grep need to be escaped to take effect. Instead of
echo abc123 | grep -o [a-z][0-9]+$
You need to write
echo abc123 | grep -o '[a-z][0-9]+$'
There are other characters that need to be escaped as well. It is also good practice to put your regex in single quotes.
You can also use egrep
which is a synonym of grep -E
and uses Extended RE, as commented by @muru.
+
in grep need to be escaped to take effect. Instead of
echo abc123 | grep -o [a-z][0-9]+$
You need to write
echo abc123 | grep -o '[a-z][0-9]+$'
There are other characters that need to be escaped as well. It is also good practice to put your regex in single quotes.
You can also use egrep
which is a synonym of grep -E
and uses Extended RE, as commented by @muru.
edited 2 days ago
answered 2 days ago
Weijun Zhou
1,468224
1,468224
Or use ERE with-E
– muru
2 days ago
The escaped+
in basic regular expressions is a non-standard GNU extension.
– Kusalananda
2 days ago
add a comment |
Or use ERE with-E
– muru
2 days ago
The escaped+
in basic regular expressions is a non-standard GNU extension.
– Kusalananda
2 days ago
Or use ERE with
-E
– muru
2 days ago
Or use ERE with
-E
– muru
2 days ago
The escaped
+
in basic regular expressions is a non-standard GNU extension.– Kusalananda
2 days ago
The escaped
+
in basic regular expressions is a non-standard GNU extension.– Kusalananda
2 days ago
add a comment |
IsaraSu is a new contributor. Be nice, and check out our Code of Conduct.
IsaraSu is a new contributor. Be nice, and check out our Code of Conduct.
IsaraSu is a new contributor. Be nice, and check out our Code of Conduct.
IsaraSu is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2funix.stackexchange.com%2fquestions%2f487484%2fwhy-regular-expression-a-z0-9-not-work%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