How do I use a regex in a shell script?

Jérôme G

I am trying to match a string with a regex in a shell script. This string is a parameter of the script ( $1 ) and it is a date (MM/DD/YYYY) The regex I'm trying to use is :

^\d{2}[\/\-]\d{2}[\/\-]\d{4}$

It seems to work, I tried it on several regex tests websites.

My shell code is :

REGEX_DATE="^\d{2}[\/\-]\d{2}[\/\-]\d{4}$"
 
echo "$1" | grep -q $REGEX_DATE
echo $?

The "echo $?" returns 1 no matter is the string I'm putting in parameter.

Do you guys have an idea ?

Thanks !

Chris Lear

I think this is what you want:

REGEX_DATE='^\d{2}[/-]\d{2}[/-]\d{4}$'

echo "$1" | grep -P -q $REGEX_DATE
echo $?

I've used the -P switch to get perl regex.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How do I use shell variables in an awk script?

From Dev

How do I use shell script to check if a bucket exists?

From Dev

How do I determine what shell a script was meant to use?

From Dev

How do I use the -ne flag for echo in a shell script?

From Dev

How do i run a shell script, in a shell script with the menu dialog?

From Dev

How do I script a filecount in android shell?

From Dev

How do I close stdin in a shell script?

From Dev

How do I script a filecount in android shell?

From Dev

How do I wait for a file in the shell script?

From Dev

How do I handle switches in a shell script?

From Dev

How do I add a line to the shell script?

From Dev

How do I use a shell-script as Chrome Native Messaging host application

From Dev

How to use regex for multiple line pattern in shell script

From Dev

How do I write shell script for Redhat Linux bash shell

From Dev

Bash shell script: how do I exit and restart the script?

From Dev

How do I use regex_replace?

From Dev

How do I use this regex with grep?

From Dev

How do I use a date in an Expect script?

From Dev

How do I use arrays in Batch script?

From Dev

How do you effectively use the 'script' command to record an interactive shell?

From Dev

How do you effectively use the 'script' command to record an interactive shell?

From Java

How can I declare and use Boolean variables in a shell script?

From Dev

How can I use vi command in shell script?

From Dev

How can I use vi command in shell script?

From Dev

How can I use the `time` command IN a shell script?

From Dev

How do I execute a string as a shell script in C++?

From Java

How do I prompt for Yes/No/Cancel input in a Linux shell script?

From Dev

How do I drop back to the python shell after exiting a script?

From Dev

How do I pass Python arrays to shell script as an Array?

Related Related

  1. 1

    How do I use shell variables in an awk script?

  2. 2

    How do I use shell script to check if a bucket exists?

  3. 3

    How do I determine what shell a script was meant to use?

  4. 4

    How do I use the -ne flag for echo in a shell script?

  5. 5

    How do i run a shell script, in a shell script with the menu dialog?

  6. 6

    How do I script a filecount in android shell?

  7. 7

    How do I close stdin in a shell script?

  8. 8

    How do I script a filecount in android shell?

  9. 9

    How do I wait for a file in the shell script?

  10. 10

    How do I handle switches in a shell script?

  11. 11

    How do I add a line to the shell script?

  12. 12

    How do I use a shell-script as Chrome Native Messaging host application

  13. 13

    How to use regex for multiple line pattern in shell script

  14. 14

    How do I write shell script for Redhat Linux bash shell

  15. 15

    Bash shell script: how do I exit and restart the script?

  16. 16

    How do I use regex_replace?

  17. 17

    How do I use this regex with grep?

  18. 18

    How do I use a date in an Expect script?

  19. 19

    How do I use arrays in Batch script?

  20. 20

    How do you effectively use the 'script' command to record an interactive shell?

  21. 21

    How do you effectively use the 'script' command to record an interactive shell?

  22. 22

    How can I declare and use Boolean variables in a shell script?

  23. 23

    How can I use vi command in shell script?

  24. 24

    How can I use vi command in shell script?

  25. 25

    How can I use the `time` command IN a shell script?

  26. 26

    How do I execute a string as a shell script in C++?

  27. 27

    How do I prompt for Yes/No/Cancel input in a Linux shell script?

  28. 28

    How do I drop back to the python shell after exiting a script?

  29. 29

    How do I pass Python arrays to shell script as an Array?

HotTag

Archive