[GRLUG] Bash regex
Nathan Phillip Brink
binki at gentoo.org
Tue May 7 14:28:18 EDT 2013
On Tue, May 07, 2013 at 01:21:35PM -0500, L. V. Lammert wrote:
> > See ^^$ info '(bash)Conditional Constructs'^^ which, in explaining the
> > regex ^^=~^^ operator, says ^^Any part of the pattern may be quoted to
> > force it to be matched as a string.^^ Your expression works after
> > removing the single quotes.
> >
> I had tried single, double, as well as no quotes, .. none would match the
> YYYY-MM string when properly formatted.
>
> I ended up with:
>
> if [[ ! $1 =~ [0-9][0-9][0-9][0-9]-[0-9][0-9] ]] ; then
> echo ' Usage: backup YYYY-MM'
> exit
> fi
>
> I believe the main problem was the missing 'number' modifier usable in
> bash, so I had to explictly repeat the digits.
>
> Thanks!
>
> Lee
Works for me:
ohnobinki at ohnopublishing ~ $ a=2012-04
ohnobinki at ohnopublishing ~ $ [[ $a =~ [0-9]{4}-[0-9]{2} ]]; echo $?
0
ohnobinki at ohnopublishing ~ $ a=20a2-04
ohnobinki at ohnopublishing ~ $ [[ $a =~ [0-9]{4}-[0-9]{2} ]]; echo $?
1
ohnobinki at ohnopublishing ~ $ echo ${BASH_VERSION}
4.2.10(2)-release
--
binki
Look out for missing or extraneous apostrophes!
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://shinobu.grlug.org/pipermail/grlug/attachments/20130507/ce0770ed/attachment.pgp>
More information about the grlug
mailing list