nirali has asked for the wisdom of the Perl Monks concerning the following question:

I want to remove forward slashes and letters attached to that forward slash from a file. I am using following code

#\/.*\(s|S)+## -> (Is this correct syntax with no quotes and nothing between the delimiters)

#\/.*\(s|S)+#""# -> (will this be the correct syntax with nothing in between the quotes ?)

Replies are listed 'Best First'.
Re: removing characters using regex
by kennethk (Abbot) on Feb 24, 2009 at 21:53 UTC

    Please read Writeup Formatting Tips, in particular the part about code tags (<c>). You'd probably also benefit from reading How (Not) To Ask A Question, in this case because showing some sample strings and their transformations would avoid ambiguity in meaning.

    Good references on dealing with regular expressions are in perlre and perlretut. Assuming you've read the string "/question Hello, how are you? /reply I'm fine." into $string and want to change that to " Hello, how are you? I'm fine.", you could use the code:

    $string =~ s/\/\S+//g;

    This will remove all occurrences of a forward slash (\/) followed by all characters which are not white space (\S+), removing such elements as \question, \reply, \45sa%%df2, .... The g tells the engine to match repeatedly.

Re: removing characters using regex
by davido (Cardinal) on Feb 24, 2009 at 21:51 UTC

    How about:

    s![[:alpha:]]*/[[:alpha:]]*!!g

    Reads as: substitute zero or more alpha characters, a / character, and zero or more trailing alpha characters with nothing.


    Dave

Re: removing characters using regex
by grizzley (Chaplain) on Feb 25, 2009 at 08:31 UTC

    Let's say you are surgeon, and you have some 'human being', which has 'appendix', like this:

    $h='human be/appendix ing'

    If you want to cut it out, you do it like this:

    $h =~ s#/appendix ##;

    That was the case with you as a perfect surgeon - no scars left. But in real world, real code, leaving scars, would be:

    $h =~ s#/appendix #~#;

    Human being has many more things to cut out, of course: kidneys, tonsils, liver:

    $h = 'h/tonsils uman /first_kidney /second_kidney b/liver e/appendix ing'

    And if you were crazy enough and willing to perform encores, you would do:

    $h =~ s#/\w+ #~#g

    It's soooo good, that surgeons have no //g flag implemented...