25142

perl find and replace ../ and  

Question:

I am using Perl to replace all instances of

../../../../../../abc' and  

in a string with

/ and  , respectively.

The method I am using looks like this:

sub encode { my $result = $_[0]; $result =~ s/..\/..\/..\/..\/..\/..\//\//g; $result =~ s/ / /g; return $result; }

Is this correct?

Answer1:

Try this. It will print /foo bar/baz.

#!/usr/bin/perl -w use strict; my $result = "../../../../../../foo bar/baz"; #$result =~ s/(\.\.\/)+/\//g; #for any number of ../ $result =~ s/(\.\.\/){6}/\//g; #for 6 exactly $result =~ s/ / /g; print $result . "\n";

Answer2:

Essentially, yes, although the first regex has to be written in a different way: because . matches any character, we have to escape it \. or put it in its own character class [.]. The first regex can also be written cleaner as

...; $result =~ s{ (?: [.][.]/ ){6} } {/}gx; ...;

We look for the literal pattern ../ repeated 6 times and then replace it. Because I use curly braces as a delimiter I don't have to escape the slash. Because I use the /x modifier I can have these spaces inside the regex improving readability.

Answer3:

you forgot the abc, i think:

sub encode { my $result = $_[0]; $result =~ s/(?:..\/){6}abc/\//g; $result =~ s/ / /g; return $result; }

Recommend

  • How to extract domain name from url?
  • python cmd completedefault() method not called
  • Eclipse > Javascript > Code highlighting not working with Object Notation
  • Get uniq random lines from file and write them to another file using php
  • Set attribute to all types in XML Schema
  • How to find MongoDB field name at arbitrary depth
  • Reading from Windows registry in Perl [duplicate]
  • File loader changed image file name but not the file name in HTML file
  • passing a default argument to a browserify module
  • Write output of for loop to multiple files
  • How to retrieve information from antrun back to maven?
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • nodemcu custom firmware build problems
  • Android Studio Can't Find tools.jar
  • Convert SQLite database to XML
  • Redirect STDERR in OPEN pipe comand. Perl Linux
  • Web.config system.webserver errors
  • cygwin cannot exec 'git-add--interactive' permission denied
  • Eclipse CDT error: Unable to compile
  • error importing numpy
  • MongoError: Incorrect arguments
  • Meteor helpers not available in Angular template
  • Read a local file using javascript
  • Is there a javascript serializer for JSON.Net?
  • Align navbar back button on right side
  • Window Size for Mac application
  • Where to put my custom functions in Wordpress?
  • Build own AppleScript numerical error handling
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • align graphs with different xlab
  • Return words with double consecutive letters
  • 0x202A in filename: Why?
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Buffer size for converting unsigned long to string
  • File not found error Google Drive API
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?