Condition string matches reg.expression
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
So, in my script, I have to decide whether one of the parameters is a valid email address.
I was trying, but it failed.
if $maddr="^.$*(@)(*)(.)(??*)"
then
...
It's meant to mean: at least 1 character followed by @ followed by anything followed by a dot and followed by something, which has at least 2 characters.
shell-script regular-expression
add a comment |Â
up vote
2
down vote
favorite
So, in my script, I have to decide whether one of the parameters is a valid email address.
I was trying, but it failed.
if $maddr="^.$*(@)(*)(.)(??*)"
then
...
It's meant to mean: at least 1 character followed by @ followed by anything followed by a dot and followed by something, which has at least 2 characters.
shell-script regular-expression
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
So, in my script, I have to decide whether one of the parameters is a valid email address.
I was trying, but it failed.
if $maddr="^.$*(@)(*)(.)(??*)"
then
...
It's meant to mean: at least 1 character followed by @ followed by anything followed by a dot and followed by something, which has at least 2 characters.
shell-script regular-expression
So, in my script, I have to decide whether one of the parameters is a valid email address.
I was trying, but it failed.
if $maddr="^.$*(@)(*)(.)(??*)"
then
...
It's meant to mean: at least 1 character followed by @ followed by anything followed by a dot and followed by something, which has at least 2 characters.
shell-script regular-expression
shell-script regular-expression
edited 27 mins ago
Rui F Ribeiro
37.3k1374118
37.3k1374118
asked Apr 28 '14 at 21:51
Wanderer
1842311
1842311
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
The Unix tool to match a string against a regexp is expr
:
if expr "$maddr" : '..*@.*...' > /dev/null; then...
(note that expr
regexps are implicitly anchored at the beginning)
Though in this case, simple shell pattern matching would be enough:
case $maddr in
?*@*.??*) ...
esac
Note that some shells like zsh
, ksh93
and bash
have builtin regexp matching operators as an extension above the standard sh
syntax, but the syntax varies slightly across those.
pattern='.@.*...'
if [[ $maddr =~ $pattern ]]; then...
Should work across all three.
(note that those patterns don't guarantee a valid email address).
second one somehow doesn't work, but first one did. thanks for your time.
â Wanderer
Apr 29 '14 at 13:01
add a comment |Â
up vote
1
down vote
_vaddr() cat
printf %s\n "$@"
grep
tries to match every line of$maddr
, not$maddr
as a whole. Also note thatecho
does some transformation on its arguments, and should be avoided for arbitrary data. With GNUgrep
you could doprintf %s "$maddr" | grep -zq "$regex"
â Stéphane Chazelas
Apr 29 '14 at 6:36
1
@StephaneChazelas - thats a very good point about echo - i should have tried a little harder. In any case - im certainly no stranger toprintf
.grep -z
i do not agree with - that is not a very portable solution. How can you have newlines in email addresses anyway? The biggest advantage to the above is you can accept streamed data and act as necessary without it first needed to be shell parsed. Not in its current form of course, but it takes very little work to get there.
â mikeserv
Apr 29 '14 at 6:52
grep -q
on non-text input is not portable either, you needprintf '%sn'
. It's about validating input so you can exclude things that are not email addresses (where anything could occur)
â Stéphane Chazelas
Apr 29 '14 at 6:54
@StephaneChezales The"$REGEX"
is just a variable and the short-circuit boolean tests either act or dont act according to their assigned function. Inclusion/Exclusion is of course at the implementer's discretion.
â mikeserv
Apr 29 '14 at 7:00
What I meant is if$maddr
may contain non-email-addresses (which we want to exclude), it may contain newline characters. Your new version does DO_SUCCESS as long as at least one address matches.
â Stéphane Chazelas
Apr 29 '14 at 7:09
 |Â
show 1 more comment
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
The Unix tool to match a string against a regexp is expr
:
if expr "$maddr" : '..*@.*...' > /dev/null; then...
(note that expr
regexps are implicitly anchored at the beginning)
Though in this case, simple shell pattern matching would be enough:
case $maddr in
?*@*.??*) ...
esac
Note that some shells like zsh
, ksh93
and bash
have builtin regexp matching operators as an extension above the standard sh
syntax, but the syntax varies slightly across those.
pattern='.@.*...'
if [[ $maddr =~ $pattern ]]; then...
Should work across all three.
(note that those patterns don't guarantee a valid email address).
second one somehow doesn't work, but first one did. thanks for your time.
â Wanderer
Apr 29 '14 at 13:01
add a comment |Â
up vote
4
down vote
accepted
The Unix tool to match a string against a regexp is expr
:
if expr "$maddr" : '..*@.*...' > /dev/null; then...
(note that expr
regexps are implicitly anchored at the beginning)
Though in this case, simple shell pattern matching would be enough:
case $maddr in
?*@*.??*) ...
esac
Note that some shells like zsh
, ksh93
and bash
have builtin regexp matching operators as an extension above the standard sh
syntax, but the syntax varies slightly across those.
pattern='.@.*...'
if [[ $maddr =~ $pattern ]]; then...
Should work across all three.
(note that those patterns don't guarantee a valid email address).
second one somehow doesn't work, but first one did. thanks for your time.
â Wanderer
Apr 29 '14 at 13:01
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
The Unix tool to match a string against a regexp is expr
:
if expr "$maddr" : '..*@.*...' > /dev/null; then...
(note that expr
regexps are implicitly anchored at the beginning)
Though in this case, simple shell pattern matching would be enough:
case $maddr in
?*@*.??*) ...
esac
Note that some shells like zsh
, ksh93
and bash
have builtin regexp matching operators as an extension above the standard sh
syntax, but the syntax varies slightly across those.
pattern='.@.*...'
if [[ $maddr =~ $pattern ]]; then...
Should work across all three.
(note that those patterns don't guarantee a valid email address).
The Unix tool to match a string against a regexp is expr
:
if expr "$maddr" : '..*@.*...' > /dev/null; then...
(note that expr
regexps are implicitly anchored at the beginning)
Though in this case, simple shell pattern matching would be enough:
case $maddr in
?*@*.??*) ...
esac
Note that some shells like zsh
, ksh93
and bash
have builtin regexp matching operators as an extension above the standard sh
syntax, but the syntax varies slightly across those.
pattern='.@.*...'
if [[ $maddr =~ $pattern ]]; then...
Should work across all three.
(note that those patterns don't guarantee a valid email address).
answered Apr 28 '14 at 22:00
Stéphane Chazelas
289k54536874
289k54536874
second one somehow doesn't work, but first one did. thanks for your time.
â Wanderer
Apr 29 '14 at 13:01
add a comment |Â
second one somehow doesn't work, but first one did. thanks for your time.
â Wanderer
Apr 29 '14 at 13:01
second one somehow doesn't work, but first one did. thanks for your time.
â Wanderer
Apr 29 '14 at 13:01
second one somehow doesn't work, but first one did. thanks for your time.
â Wanderer
Apr 29 '14 at 13:01
add a comment |Â
up vote
1
down vote
_vaddr() cat
printf %s\n "$@"
grep
tries to match every line of$maddr
, not$maddr
as a whole. Also note thatecho
does some transformation on its arguments, and should be avoided for arbitrary data. With GNUgrep
you could doprintf %s "$maddr" | grep -zq "$regex"
â Stéphane Chazelas
Apr 29 '14 at 6:36
1
@StephaneChazelas - thats a very good point about echo - i should have tried a little harder. In any case - im certainly no stranger toprintf
.grep -z
i do not agree with - that is not a very portable solution. How can you have newlines in email addresses anyway? The biggest advantage to the above is you can accept streamed data and act as necessary without it first needed to be shell parsed. Not in its current form of course, but it takes very little work to get there.
â mikeserv
Apr 29 '14 at 6:52
grep -q
on non-text input is not portable either, you needprintf '%sn'
. It's about validating input so you can exclude things that are not email addresses (where anything could occur)
â Stéphane Chazelas
Apr 29 '14 at 6:54
@StephaneChezales The"$REGEX"
is just a variable and the short-circuit boolean tests either act or dont act according to their assigned function. Inclusion/Exclusion is of course at the implementer's discretion.
â mikeserv
Apr 29 '14 at 7:00
What I meant is if$maddr
may contain non-email-addresses (which we want to exclude), it may contain newline characters. Your new version does DO_SUCCESS as long as at least one address matches.
â Stéphane Chazelas
Apr 29 '14 at 7:09
 |Â
show 1 more comment
up vote
1
down vote
_vaddr() cat
printf %s\n "$@"
grep
tries to match every line of$maddr
, not$maddr
as a whole. Also note thatecho
does some transformation on its arguments, and should be avoided for arbitrary data. With GNUgrep
you could doprintf %s "$maddr" | grep -zq "$regex"
â Stéphane Chazelas
Apr 29 '14 at 6:36
1
@StephaneChazelas - thats a very good point about echo - i should have tried a little harder. In any case - im certainly no stranger toprintf
.grep -z
i do not agree with - that is not a very portable solution. How can you have newlines in email addresses anyway? The biggest advantage to the above is you can accept streamed data and act as necessary without it first needed to be shell parsed. Not in its current form of course, but it takes very little work to get there.
â mikeserv
Apr 29 '14 at 6:52
grep -q
on non-text input is not portable either, you needprintf '%sn'
. It's about validating input so you can exclude things that are not email addresses (where anything could occur)
â Stéphane Chazelas
Apr 29 '14 at 6:54
@StephaneChezales The"$REGEX"
is just a variable and the short-circuit boolean tests either act or dont act according to their assigned function. Inclusion/Exclusion is of course at the implementer's discretion.
â mikeserv
Apr 29 '14 at 7:00
What I meant is if$maddr
may contain non-email-addresses (which we want to exclude), it may contain newline characters. Your new version does DO_SUCCESS as long as at least one address matches.
â Stéphane Chazelas
Apr 29 '14 at 7:09
 |Â
show 1 more comment
up vote
1
down vote
up vote
1
down vote
_vaddr() cat
printf %s\n "$@"
_vaddr() cat
printf %s\n "$@"
edited Apr 29 '14 at 7:12
answered Apr 29 '14 at 0:11
mikeserv
44.7k565151
44.7k565151
grep
tries to match every line of$maddr
, not$maddr
as a whole. Also note thatecho
does some transformation on its arguments, and should be avoided for arbitrary data. With GNUgrep
you could doprintf %s "$maddr" | grep -zq "$regex"
â Stéphane Chazelas
Apr 29 '14 at 6:36
1
@StephaneChazelas - thats a very good point about echo - i should have tried a little harder. In any case - im certainly no stranger toprintf
.grep -z
i do not agree with - that is not a very portable solution. How can you have newlines in email addresses anyway? The biggest advantage to the above is you can accept streamed data and act as necessary without it first needed to be shell parsed. Not in its current form of course, but it takes very little work to get there.
â mikeserv
Apr 29 '14 at 6:52
grep -q
on non-text input is not portable either, you needprintf '%sn'
. It's about validating input so you can exclude things that are not email addresses (where anything could occur)
â Stéphane Chazelas
Apr 29 '14 at 6:54
@StephaneChezales The"$REGEX"
is just a variable and the short-circuit boolean tests either act or dont act according to their assigned function. Inclusion/Exclusion is of course at the implementer's discretion.
â mikeserv
Apr 29 '14 at 7:00
What I meant is if$maddr
may contain non-email-addresses (which we want to exclude), it may contain newline characters. Your new version does DO_SUCCESS as long as at least one address matches.
â Stéphane Chazelas
Apr 29 '14 at 7:09
 |Â
show 1 more comment
grep
tries to match every line of$maddr
, not$maddr
as a whole. Also note thatecho
does some transformation on its arguments, and should be avoided for arbitrary data. With GNUgrep
you could doprintf %s "$maddr" | grep -zq "$regex"
â Stéphane Chazelas
Apr 29 '14 at 6:36
1
@StephaneChazelas - thats a very good point about echo - i should have tried a little harder. In any case - im certainly no stranger toprintf
.grep -z
i do not agree with - that is not a very portable solution. How can you have newlines in email addresses anyway? The biggest advantage to the above is you can accept streamed data and act as necessary without it first needed to be shell parsed. Not in its current form of course, but it takes very little work to get there.
â mikeserv
Apr 29 '14 at 6:52
grep -q
on non-text input is not portable either, you needprintf '%sn'
. It's about validating input so you can exclude things that are not email addresses (where anything could occur)
â Stéphane Chazelas
Apr 29 '14 at 6:54
@StephaneChezales The"$REGEX"
is just a variable and the short-circuit boolean tests either act or dont act according to their assigned function. Inclusion/Exclusion is of course at the implementer's discretion.
â mikeserv
Apr 29 '14 at 7:00
What I meant is if$maddr
may contain non-email-addresses (which we want to exclude), it may contain newline characters. Your new version does DO_SUCCESS as long as at least one address matches.
â Stéphane Chazelas
Apr 29 '14 at 7:09
grep
tries to match every line of $maddr
, not $maddr
as a whole. Also note that echo
does some transformation on its arguments, and should be avoided for arbitrary data. With GNU grep
you could do printf %s "$maddr" | grep -zq "$regex"
â Stéphane Chazelas
Apr 29 '14 at 6:36
grep
tries to match every line of $maddr
, not $maddr
as a whole. Also note that echo
does some transformation on its arguments, and should be avoided for arbitrary data. With GNU grep
you could do printf %s "$maddr" | grep -zq "$regex"
â Stéphane Chazelas
Apr 29 '14 at 6:36
1
1
@StephaneChazelas - thats a very good point about echo - i should have tried a little harder. In any case - im certainly no stranger to
printf
. grep -z
i do not agree with - that is not a very portable solution. How can you have newlines in email addresses anyway? The biggest advantage to the above is you can accept streamed data and act as necessary without it first needed to be shell parsed. Not in its current form of course, but it takes very little work to get there.â mikeserv
Apr 29 '14 at 6:52
@StephaneChazelas - thats a very good point about echo - i should have tried a little harder. In any case - im certainly no stranger to
printf
. grep -z
i do not agree with - that is not a very portable solution. How can you have newlines in email addresses anyway? The biggest advantage to the above is you can accept streamed data and act as necessary without it first needed to be shell parsed. Not in its current form of course, but it takes very little work to get there.â mikeserv
Apr 29 '14 at 6:52
grep -q
on non-text input is not portable either, you need printf '%sn'
. It's about validating input so you can exclude things that are not email addresses (where anything could occur)â Stéphane Chazelas
Apr 29 '14 at 6:54
grep -q
on non-text input is not portable either, you need printf '%sn'
. It's about validating input so you can exclude things that are not email addresses (where anything could occur)â Stéphane Chazelas
Apr 29 '14 at 6:54
@StephaneChezales The
"$REGEX"
is just a variable and the short-circuit boolean tests either act or dont act according to their assigned function. Inclusion/Exclusion is of course at the implementer's discretion.â mikeserv
Apr 29 '14 at 7:00
@StephaneChezales The
"$REGEX"
is just a variable and the short-circuit boolean tests either act or dont act according to their assigned function. Inclusion/Exclusion is of course at the implementer's discretion.â mikeserv
Apr 29 '14 at 7:00
What I meant is if
$maddr
may contain non-email-addresses (which we want to exclude), it may contain newline characters. Your new version does DO_SUCCESS as long as at least one address matches.â Stéphane Chazelas
Apr 29 '14 at 7:09
What I meant is if
$maddr
may contain non-email-addresses (which we want to exclude), it may contain newline characters. Your new version does DO_SUCCESS as long as at least one address matches.â Stéphane Chazelas
Apr 29 '14 at 7:09
 |Â
show 1 more comment
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f126989%2fcondition-string-matches-reg-expression%23new-answer', 'question_page');
);
Post as a guest
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
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
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