Question: I've 2 array list having the same size named as "name" and "path" containing the name and path of the files respectively (which I got from reading external storage using a search function which I made).).

Now after, I've sorted the "name" array list according to alphabetical order case insensitive but the "path" array list remains the same.

I want to use these array lists in my list view, but the problem is when I click any file from listview and as we know that only one array list of "name" was sorted so they are not opening the correct file from its corresponding path because the "path" array list still contains the path of the old order of the "name" list.

So how can I make the "path" array list in the same order as the "name" list? In order to match new name list with their correct path.

If possible can someone help me through Teamviewer? I can't post the entire code here as this is my University project, I hope you all can understand.

Answer: 
First, don't try and manipulate CSV (or XML or HTML) with regexes. While CSV might seem simple, it can be subtle. Instead use [Text::CSV](https://metacpan.org/pod/Text::CSV). The exception is if your CSV is malformed and you're fixing it.

Now, for what your regex is doing. First, let's translate it it from `s//` to `s{}{}` which is a bit easier on the eyes and use `\x` so we can space things out a bit.

```
s{
    # Capture to $1
    (
        # A comma.
        ,
        # 0 or more `h` "horizontal whitespace": tabs and spaces
        \h*
        # A quote.
        "
        # 0 or more of anything which is not a quote or newline.
        [^\n"]*
    )
    # A newline (not captured)
    \n
}
# Put the captured bit in with a space after it.
# The `g` says to do it multiple times over the whole string.
{$1 }gx

```

It will change `foo, "bar\n` into `foo, "bar`. I'm guessing it's turning text fields in the CSV with newlines in them into ones with just spaces.

```
foo, "first
field", "second
field"

```

Will become

```
foo, "first field", "second field"

```

---

This is something better handled with Text::CSV. I suspect the purpose of the transform is to help out CSV parsers which cannot handle newlines. [Text::CSV can with a little coercing](https://metacpan.org/pod/Text::CSV#Embedded-newlines).

```
#!/usr/bin/env perl

use strict;
use warnings;
use v5.10;
use autodie;

use Text::CSV;
use IO::Scalar;
use Data::Dumper;

# Pretend our scalar is an IO object so we can use `getline`.
my $str = qq[foo, "bar", "this\nthat"\n];
my $io = IO::Scalar->new(\$str);

# Configure Text::CSV
my $csv = Text::CSV->new({
    # Embedded newlines normally aren't allowed, this tells Text::CSV to
    # treat the content as binary instead.
    binary=> 1,

    # Allow spaces between the cells.
    allow_whitespace => 1
});

# Use Text::CSV->getline() to do the parsing.
while( my $row = $csv->getline($io) ) {
    # Dump the contents of the row
    say Dumper $row;
}

```

And it will correctly parse the row and its embedded newlines.

```
$VAR1 = [
          'foo',
          'bar',
          'this
that'
        ];

```
*Edited this to second Schwern (also upvoted): regular expressions seem to be a poor fit for manipulating CSV.*

As for the regular expression in question, let's dissect it. Starting with the top level:

`'s/(,\h*"[^\n"]*)\n/$1 /g'`

The `s/part1/part2/g` expression means "substitute the first part with the second part everywhere".

Now let's examing the "first part":

`(,\h*"[^\n"]*)\n`

The parentheses are enclosing a group. There is only one group, so it becomes group number 1. We'll come back to that in the next step.

Then, check out <https://perldoc.perl.org/perlrebackslash.html> for explanation of the character classes. `\h` is a horizontal whitespace and `\n` is a logical newline character.

The expression inside the group is stating: "starts with a comma, then any number of horizontal whitespace characters, then anything but a newline and quote; finally, there must be a trailing newline". So it is basically a comma follwed by a csv field.

Lastly, the "second part" reads:

`$1`

This is just a reference to the group number 1 that was captured earlier followed by a space.

In overall, the whole expression replaces a trailing string field that is not terminated with a quote and removing it's newline terminator.