Determine the string is valid

Dear All,

I have a string "1234567899*0#123456789#", it can be divided into:

  • 1st: 1234567899 Validation: 10 digits, only 0-9
  • 2nd: *0# Fixed Value
  • 3rd: 123456789 Validation: 9 digits, only 0-9
  • 4th: # Fixed Value

Would like to know if any 1 line statement perl, awk, sed can do this checking.

Thanks

perl -e 'if($ARGV[0] =~ /\A[0-9]{10}\*0#[0-9]{9}#\z/){print "Valid string\n"}else{print "Invalid string\n"}' '1234567899*0#123456789#'
1 Like

Thanks elixir, it works perfect.
regarding the \A, and \z, would u please guide me those two meaning.

Both of these are anchors which force a match to be performed at that particular position. \A is the beginning-of-string anchor (absolute) and \z is the absolute end-of-string anchor.

Thanks elixir, learnt a lot. :b:

[ `echo '1234567899*0#123456789#' | egrep '^[0-9]{10}\*0#[0-9]{9}#'` ] && echo good || echo bad