Jump to content

[SOLVED] Bitwise negation with regular expression question


noochies

Recommended Posts

Hello,

I am very baffled with some lines of code from a program I took over that was written by someone else.

Here is the code:

 

my $ext=$file;
$ext =~ /(\..{3,4}$)/;
$ext = $1;
my $documentFormatId = isAllowedExtension($ext);

 

I know that this is ultimately getting the file extension into the $ext variable, but that's as far as I've gotten.

 

I can't figure out what the second line is trying to do. I put in print statements in between each of those lines printing out what the var $ext had in it. After the second line, the var is the same as it was before the second line (it equals $file).

 

I know that the tilde character (~) is the bitwise negation character, but I'm not sure how the regular expression fits into the mix.

Can someone tell me how you bitwise negate a string with a regular expresson involved?

 

Link to comment
Share on other sites

This is Perl:

 

### Define the variable
my $ext = $file;
### Run a match
$ext =~ /(\..{3,4}$)/;
### Assign the first capture from the last match
$ext = $1;

NODE                    EXPLANATION

----------------------------------------------------------------------

  (                        group and capture to \1:

----------------------------------------------------------------------

    \.                      '.'

----------------------------------------------------------------------

    .{3,4}                  any character except \n (between 3 and 4

                            times (matching the most amount

                            possible))

----------------------------------------------------------------------

    $                        before an optional \n, and the end of

                            the string

----------------------------------------------------------------------

  )                        end of \1

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.