Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegEx Woes 3

Status
Not open for further replies.

1DMF

Programmer
Joined
Jan 18, 2005
Messages
8,795
Location
GB
I can't seem to get the syntax right on this regex

my $var =~ s/['\@\\\/=?]//g;

I want to replace the following characters with nothing
' @ \ / = ?

thanks 1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 

Brackets around charactors mean to match one or more of the charactors in the brackets, in any position or sequence.

Try this:

my $var = "abc'\@\\\/=?xyz";
$var =~ s/'\@\\\/=\?//g;

print "$var\n";
 

Correction, brackets mean to match ONE of the charactors.
 
Don't use `my' in the regexp line or it tries to redeclare $var. If you've warnings enabled, it should complain about that. The regexp itself is fine (though there's no need to escape the @ symbol).

Another (more efficient) way of doing that:
Code:
$var =~ tr/'@\\\/=?//d;
 

Oh, the charactors individually, not the string? I don't think you need to escape charactors inside brackets.
Sorry, I'm really out of it today.
 
hey ishnid, i wasn't actual declaring the var with my it was a habit when posting to always put my, stops the moaning about not using strict and declaring vars ;-) (which I 100% now do) I even find myself writing 'my' when posting in the JavaScript or other forums, it's that engrained now - lol

the actual reason I was not getting a match was a stupid school boy error, the regex works fine!

I was doing this...
Code:
my $userid = $cgi->param('userid');
$userid =~ s/['\@\\\/\=?]//g;
if($userid ne $cgi->param('userid')){
    $msg .= "UserID : ('@\\/=?) not allowed.<br />";
}
but the dumb ass that I am forgot the form has <input name="USERID"> d'oh!

that's why it didn't work, got the case wrong - lol , but have a star anyway!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
seems like an unusual way to go about that, this is a more straight forward approach:

Code:
my $userid = $cgi->param('userid');
if ($userid =~ /['@\\\/=?]/;){
    $msg .= "UserID : ('@\\/=?) not allowed.<br />";
}

but that still leaves plenty of non alpha-numeric characters that can be included.
 
oops, that ';' shouldn't be in this line:

Code:
if ($userid =~ /['@\\\/=?]/;){

should be:

Code:
if ($userid =~ /['@\\\/=?]/){
 
You just woke up Kevin?, how does that meet my requesite
I want to replace the following characters with nothing ' @ \ / = ?

and you can't run a replace/substitute on $cgi as it is read only, so why is my method strange?

:-P



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
it's unusual in the context of the code you posted:

Code:
if($userid ne $cgi->param('userid')){
    $msg .= "UserID : ('@\\/=?) not allowed.<br />";
}

but it might make perfect sense in the overall script.
 
it's a post back to an AJAX JavaScript call, complaining about invalid submision and removing it at the same time!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
'my' with JavaScript - bet that confuses the people on *that* forum. Glad you got it sorted. Cheers for the star.
 
I sometimes catch myself before actualy submitting, what the JS forum doesn't do though is moan at you for not using var variablename = "xyz"; , so i've learnt to be a better PERL coder than JS thanks to you guys :-)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
i gave your example a try Kevin as I decided not to replace the chars and simply just flag up the invalid chars but your code doesn't work.

why does a substitute work but not a match using the same regex?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
the code will find the invalid characters, for example:

Code:
my $userid = 'foo@bar';
if ($userid =~ /['@\\\/=?]/){
    print "UserID : ('@\\/=?) not allowed.<br />";
}
else {
   print "UserID is OK.<br />";
}
 
agh it must have been because I was using the CGI->param object the match didn't work as i was doing..
Code:
if ($cgi->param('userid') =~ /['@\\\/=?]/){
    print "UserID : ('@\\/=?) not allowed.<br />";
}

It was always true with this, do i need $$cgi->param('userid') or can i not use the $cgi object with a match in this manner ?


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
>> or can i not use the $cgi object with a match in this manner

No you can't because that is a reference to the cgi object, you have to dereference it first.
 
Status
Not open for further replies.

Similar threads

Replies
2
Views
347
  • Locked
  • Question Question
Replies
4
Views
468
  • Locked
  • Question Question
Replies
1
Views
308
  • Locked
  • Question Question
Replies
5
Views
450

Part and Inventory Search

Sponsor

Back
Top