Ilia Ross
Ilia Ross

Reputation: 13412

Perl parsing JavaScript file regex, to catch quotes only at the beginning and end of the returned string

I'm just starting to learn Perl. I need to parse JavaScript file. I came up with the following subroutine, to do it:

sub __settings {
    my ($_s) = @_;
    my $f = $config_directory . "/authentic-theme/settings.js";
    if ( -r $f ) {
        for (
            split(
                '\n',
                $s = do {
                    local $/ = undef;
                    open my $fh, "<", $f;
                    <$fh>;
                    }
            )
            )
        {
            if ( index( $_, '//' ) == -1
                && ( my @m = $_ =~ /(?:$_s\s*=\s*(.*))/g ) )
            {
                my $m = join( '\n', @m );
                $m =~ s/[\'\;]//g;
                return $m;
            }
        }
    }
}

I have the following regex, that removes ' and ; from the string:

s/[\'\;]//g;

It works alright but if there is a mentioned chars (' and ;) in string - then they are also removed. This is undesirable and that's where I stuck as it gets a bit more complicated for me and I'm not sure how to change the regex above correctly to only:

  1. Remove only first ' in string
  2. Remove only last ' in string
  3. Remove ont last ; in string if exists

Any help, please?

Upvotes: 4

Views: 119

Answers (4)

Borodin
Borodin

Reputation: 126722

I suggest that your original code should look more like this. It is much more idiomatic Perl and I think more straightforward to follow

sub __settings {
    my ($_s) = @_;
    my $file = "$config_directory/authentic-theme/settings.js";
    return unless -r $file;

    open my $fh, '<', $file or die qq{Unable to open "$file" for input: $!};
    my @file = <$fh>;
    chomp @file;

    for ( @file ) {
        next if m{//};
        if ( my @matches = $_ =~ /(?:$_s\s*=\s*(.*))/g ) {
            my $matches = join "\n", @matches;
            $matches =~ tr/';//d;
            return $matches;
        }
    }
}

Upvotes: 1

karthik manchala
karthik manchala

Reputation: 13640

You can use the following to match:

^'|';?$|;$

And replace with '' (empty string)

See DEMO

Upvotes: 3

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626893

You can use this code:

#!/usr/bin/perl
$str = "'string; 'inside' another;"; 
$str =~ s/^'|'?;?$//g;
print $str;

IDEONE demo

The main idea is to use anchors: ^ beginning of string, $ end of string and ;? matches the ";" symbol at the end only if it is present (? quantifier is making the pattern preceding it optional).
EDIT: Also, ; will get removed even if there is no preceding '.

Upvotes: 2

vks
vks

Reputation: 67968

Remove only first ' in string

Remove only last ' in string

^[^']*\K'|'(?=[^']*$)

Try this .See demo.

https://regex101.com/r/oF9hR9/8

Remove ont last ; in string if exists

;(?=[^;]*$)

Try this.See demo.

https://regex101.com/r/oF9hR9/9

All three in one

^[^']*\K'|'(?=[^']*$)|;(?=[^;]*$)

See Here

Upvotes: 2

Related Questions