in reply to String replacement

i think it's best to use the position match and then create a substr
Why? If you could use index (or rindex) to find the right position to use for substr, why not? But, with your data, you'll most probably use a regular expression to find what you want to delete, you might as well use the s/// substitution operator to delete what you don't want.

But that's sort of theoretical, I really can't say very much without having seen samples of your input data and desired output.

Replies are listed 'Best First'.
Re^2: String replacement
by kepler (Scribe) on Sep 14, 2015 at 11:18 UTC

    Hi, I think the following regex/substitution is working...

    $loop_variable =~ s/ ?[A-Za-z]{1} {1}(\[(.*)\])? ?\d+(.*)?\.+(.*)?//g;

    Had quite a bit of work though...

      Yes, maybe it works, but I can't comment much on it without having seen the data, which you haven't shown yet.

      One thing, though: if you need an atom or a sub-pattern once and only once, then you don't need a quantifier such as {1}, that's what the regex engine will do anyway by default. So that this slightly simpler substitution should do the same thing:

      $loop_variable =~ s/ ?[A-Za-z] (\[(.*)\])? ?\d+(.*)?\.+(.*)?//g;
      I believe the rest of the regex could probably be improved. For example something like .* is rarely a good idea (.*? is often better), and (\[(.*)\])? is probably better written as (\[(.*?)\])? or as (\[[^]]*\])? (untested). That's just one example.

      But I would need to see the data and to know what exactly you find significant in the strings you are trying to match (what are the invariants that you are looking for and what are the variable parts) before I could really give more informed advice.