0

Say, I have lines like:

SOMETHING.AA.AA.DARKSIDE

BLaH.AA.AA.Blah

I want to find for each line $before = $1; $after = $2; of the $middle = ”AA” Such that for example for line 1 I get:

$before= “SOMETHING.”
$After = “.AA.DARKSIDE”

And also

$before= “SOMETHING.AA”
$After = “.DARKSIDE”

My code looks like this:

$middle = “AA”;  

foreach (@lines){

   $line = $_;   

   while ($line =~m/^(.+)$middle(.+)$/g){

      $before = $1;
      $after  = $2;
  }
}

Is there a simple way to change regex in my while? PS: $middle will be a variable so i cannot hardcode it. Thank you for help.

Alma Do
  • 37,009
  • 9
  • 76
  • 105
yurekino
  • 3
  • 1
  • can you give an example of what you want your output to look like? – philshem Sep 30 '13 at 09:39
  • Does it work now, and you are only asking if there is a better way? – mavrosxristoforos Sep 30 '13 at 09:40
  • Can you have something like `SOMETHING.AA.AA.AA.DARKSIDE` as well? If so, are you expecting 3 possible outputs? – Jerry Sep 30 '13 at 09:41
  • If it helps, there will be maximum 2 occurances of (for example).AA. most of the time there will be SOMETHING.AA.DARKSIDE. The current regex will only find one case out of SOMETHING.AA.AA.DARKSIDE. – yurekino Sep 30 '13 at 10:14
  • Later i need to construct an animal like:SOMETHING.AA.AA.DARKSIDE = SOMETHING.BB.AA.DARKSIDE+SOMETHING.CC.AA.DARKSIDE but also SOMETHING.AA.AA.DARKSIDE = SOMETHING.AA.BB.DARKSIDE+SOMETHING.AA.CC.DARKSIDE. Thanks. – yurekino Sep 30 '13 at 10:24

1 Answers1

0

Why do you want to use regexes for that?

($before, $after) = split(/$middle\.$middle/, $line);

And then use $before and $after each once without and once with $middle concatenated to the end and start of the string respectively.

Vince
  • 1,517
  • 2
  • 18
  • 43
  • Thanks a lot Vince, I will do it your way. I was hoping to have a one line solution, but realised (by browsing other posts) that it is rather impossible. – yurekino Sep 30 '13 at 14:16
  • Glad I could help. Would you mind accepting my answer then? :) – Vince Sep 30 '13 at 14:45