Results 1 to 5 of 5

Thread: Regular expression to validate that parameter is a number

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default Regular expression to validate that parameter is a number

    Hello, I have this script and I'm not going to bore you with what it does, but the user must enter 1 or more octal numbers into it and the script has to validate it.

    I have this regular expression, against which all parameters will be validated:
    Code:
    isOctal='^[0-7]+$'
    When I enter one number, it goes through fine, but when I enter two or more, it doesn't. The way I check parameters against the regex above is:
    Code:
    if [[ ! $* =~ $isOctal ]]; then
            echo "Number(s) entered not in octal format"
    Could it be in the way I try to use "$*" to check ALL parameters or is it my regex? Thanks beforehand.
    Last edited by fedoraman; 16th November 2012 at 07:14 PM.

  2. #2
    Senior Member Rahul.Patil's Avatar
    Join Date
    Feb 2012
    Location
    Mumbai india
    Posts
    486
    Thanks
    10
    Thanked 50 Times in 47 Posts
    Rep Power
    8

    Default

    Hi,

    Regex should be in Double Quoted , and keep double quotes to "$*"
    Rahul Patil <http://www.linuxian.com>

  3. #3
    Senior Member
    Join Date
    Aug 2011
    Posts
    458
    Thanks
    0
    Thanked 62 Times in 58 Posts
    Rep Power
    10

    Default

    hi,

    Regex should be in Double Quoted
    no. isoctal='^[0-7]$' is fine. and right hand side of test should not be quoted.

    I'm lacking some context. what's a typical input?
    Code:
    myFunction() { isoctal='^[0-7]$'; [[ $1 =~ $isoctal ]] && echo ok || echo ko;}
    myFunction 15 5 9
    ko
    because of 15 and 9
    Code:
    myFunction 5 6 4
    ok
    A problem clearly stated is a problem half solved.

  4. #4
    Junior Member
    Join Date
    Aug 2012
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Quote Originally Posted by Watael View Post
    hi,

    no. isoctal='^[0-7]$' is fine. and right hand side of test should not be quoted.

    I'm lacking some context. what's a typical input?
    Code:
    myFunction() { isoctal='^[0-7]$'; [[ $1 =~ $isoctal ]] && echo ok || echo ko;}
    myFunction 15 5 9
    ko
    because of 15 and 9
    Code:
    myFunction 5 6 4
    ok
    okay the problem was solved by making the regex as follows:

    Code:
    isOctal='^[ 0-7]$'
    my teacher helped me with this; apparently when you do "echo $*", it doesn't output seperate positional parameters, but it outputs one line with all the parameters in one piece, therefore, the regular expression would see the spaces in the line between the parameters and would not go through.

  5. #5
    Senior Member
    Join Date
    Aug 2011
    Posts
    458
    Thanks
    0
    Thanked 62 Times in 58 Posts
    Rep Power
    10

    Default

    oops
    I did some attempts with $1 and forgot it wasn't $* anymore
    A problem clearly stated is a problem half solved.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 17th September 2012, 11:27 AM
  2. Regular Expression Discussion
    By eawedat in forum Shell scripting
    Replies: 2
    Last Post: 16th August 2011, 05:43 PM
  3. [Solved] Matched characters - regular expression
    By jianma in forum Shell scripting
    Replies: 2
    Last Post: 17th June 2011, 03:03 AM
  4. [Solved] Regular expression for SED to match spaces and alphanumeric characters
    By vargasmas in forum Shell scripting
    Replies: 1
    Last Post: 10th July 2010, 02:51 PM
  5. Replies: 0
    Last Post: 8th March 2010, 03:48 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •