perl regex help needed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl regex help needed
# 1  
Old 09-16-2012
perl regex help needed

Hi,

I want to validate strings in perl, the string may contains characters from a-zA-Z0-9 and symbols +-_.:/\

To validate such a string I computed a regex

Code:
if ($string =~ m/^[a-zA-Z0-9+-_.:\/\\]/) {
   print "valid";
} else {
   print "invalid";
}

but this regex also validates strings that contain other characters like @%^&*?.

I want to disallow strings which contain all other characters from above valid set.

plz help me

thanks,
zing_foru

Last edited by Scott; 09-16-2012 at 08:47 AM.. Reason: Code tags
# 2  
Old 09-16-2012
Code:
if ($string =~ m{\A[-+.:/\\a-zA-Z0-9_]+\z}) {
print "valid";
} else {
print "invalid";
}

and if you have Perl 5.14+, you could replace the first line with:
Code:
if ($string =~ m{\A[-+.:/\\\w]+\z}a) {


Last edited by elixir_sinari; 09-16-2012 at 12:24 PM..
# 3  
Old 09-16-2012
If an empty string is allowed, you could also negate the bracketed expression, rendering anchors unnecessary.

Regards,
Alister
# 4  
Old 09-16-2012
thanks elixir_sinari, but this regex even not validating correct strings
Code:
e.g. "e:\test1\test2"
      "/usr/bin/test"
      "/var/log-test"
      "/usr/test bin/goak"

invalid strings e.g.

Code:
"e:\test\test?newdata"  #contains ? invalid symbol
"goak&<>"  #contains & <> invalid symbol


Last edited by Scott; 09-16-2012 at 08:48 AM.. Reason: Please use code tags
# 5  
Old 09-16-2012
Try this Reg Exp

Code:
if($string =~ m/^[\w\s+-.:\/\\]*$/)
{
print "Valid";
}else {
print "Invalid"
}

# 6  
Old 09-16-2012
MySQL

thanks msabhi.. worked Smilie
# 7  
Old 09-16-2012
Quote:
Originally Posted by zing_foru
thanks msabhi.. worked Smilie
No, it did not. You just did not notice the error. A comma is still allowed even though it's not on your list of allowed characters.

The portions highlighted in red represent ranges of characters, not the three characters which appear literally in the regular expression.

Quote:
Originally Posted by zing_foru
I want to validate strings in perl, the string may contains characters from a-zA-Z0-9 and symbols +-_.:/\
Code:
/^[a-zA-Z0-9+-_.:\/\\]/

but this regex also validates strings that contain other characters like @%^&*?

Quote:
Originally Posted by msabhi
Try this Reg Exp
Code:
/^[\w\s+-.:\/\\]*$/

Neither of those is correct. msabhi's example is simply less wrong. zing_foru's unintended range expression spans the entirety of the following list. msabhi's attempt spans the highlighted section.

From POSIX - Locale - collation sequence
Code:
<plus-sign>
<comma>
<hyphen>
<period>
<slash>
<zero>
<one>
<two>
<three>
<four>
<five>
<six>
<seven>
<eight>
<nine>
<colon>
<semicolon>
<less-than-sign>
<equals-sign>
<greater-than-sign>
<question-mark>
<commercial-at>
<A>
<B>
<C>
<D>
<E>
<F>
<G>
<H>
<I>
<J>
<K>
<L>
<M>
<N>
<O>
<P>
<Q>
<R>
<S>
<T>
<U>
<V>
<W>
<X>
<Y>
<Z>
<left-square-bracket>
<backslash>
<right-square-bracket>
<circumflex>
<underscore>

To fix your errors, refer to the perlre man page @ perldoc.perl.org
Quote:
Within a list, the "-" character specifies a range, so that a-z represents all characters between "a" and "z", inclusive. If you want either "-" or "]" itself to be a member of a class, put it at the start of the list (possibly after a "^"), or escape it with a backslash. "-" is also taken literally when it is at the end of the list, just before the closing "]".
Regards,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl REGEX help

Experts - I found a script on one of the servers that I work on and I need help understanding one of the lines. I know what the script does, but I'm having a hard time understanding the grouping. Can someone help me with this? Here's the script... #!/usr/bin/perl use strict; use... (2 Replies)
Discussion started by: timj123
2 Replies

2. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

3. Programming

Perl regex

Hello folks, Looking for a quick help on regex in my perl script. here's the string i want to parse and get the 2nd field out of it. $str = " 2013-08-07 12:29 Beta ACTIVE"; I want to extract 'Beta' out of this string. This string will keep on changing... (2 Replies)
Discussion started by: jhamaks
2 Replies

4. Programming

Perl regex

Hello, I'm trying to get a quick help on regex since i'm not a regular programmer. Below is the line i'm trying to apply my regex to..i want to use the regex in a for loop and this line will keep on changing. subject=... (4 Replies)
Discussion started by: jhamaks
4 Replies

5. UNIX for Dummies Questions & Answers

RegEX help needed

Hi, Have to filter out string before the last underscore in the following input: UNIX_Solaris_59_KSH output: UNIX_Solaris_59 dummy one but :mad: Thanks & Regards, Sourabh Singh Khichi (4 Replies)
Discussion started by: skhichi
4 Replies

6. UNIX for Dummies Questions & Answers

Regex Needed:(

I am looking for the proper regex to match the hostname "areagc11" of this log.... Any help would be awsome:) Oct 25 11:08:18 areagc11 961: Oct 25 18:08:17.536 GMT: %SYS-5-CONFIG_I: Configured from console by someone onvty1 (10.156.72.97) (6 Replies)
Discussion started by: jlaigo2
6 Replies

7. Shell Programming and Scripting

Help needed in regex

Hi, Could you please help me in writing a regex for the following requirement? Let following be the string format: abc.cdef.ghij.lm I need to check between dots, there is atleast one character{a-z,A-Z,*}. Eg: abc1.gt2.345j is valid, but not 123.abc.vff.gth because 123 should not be... (2 Replies)
Discussion started by: lorzinian
2 Replies

8. Shell Programming and Scripting

Regex help needed

Hello, I'd like to write a regex that transforms a German base form of a noun into one of its inflected forms, namely I want to translate "Haus" to "Häuser" This is what I've got: /^(.+)$/_Umlaut( $1 )_er/ where _Umlaut( x )_ is a function operating on the noun stem captured by $1 The... (1 Reply)
Discussion started by: Bloomy
1 Replies

9. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

10. Shell Programming and Scripting

Sed and regex help needed

Hi all, I'm writing a script that replaces a value in a file. The file is formatted as follows: So, for this example, I'd like to replace the value for param_two. The value for param_two can be a one, or two-digit number. It replaces the value in file.cfg, and directs the... (9 Replies)
Discussion started by: marknu1
9 Replies
Login or Register to Ask a Question