in reply to Re: String replacement
in thread String replacement

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...

Replies are listed 'Best First'.
Re^3: String replacement
by Laurent_R (Canon) on Sep 14, 2015 at 13:52 UTC
    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.