The only function of the commas between the entries is to define the elements of a list - you need () not {} to define a list BTW. I assume you are presenting a list as what you do present will not compile. Once we assign to the @article array the commas between the elements are discarded as they have done their job of separating the list elements.

Thus we can do what you ask like this:

my @article = ( 'author = {Author, J.P }' , 'title = "A paper about things"' , 'etc...' ); # iterate over @article array removing all commas # the commas in the list assignment are gone having # been used to assign the list elements to the array # @article so we have no problem with them now. s/,//g for @article; # this is the same as foreach (@article) { $_ =~ s/,//g; } # in less idiomatic Perl where we do not take advantage of # the aliasing of array elements to $_ we have to write for my $i (0..$#article) { $article[$i] =~ s/,//g; } # to print all the elements of a list # on newlines I would normally just write: print "$_\n" for @article; # this takes advantage of the aliasing to $_ in a for loop # alternatively in long perl foreach my $stuff (@article) { print "$stuff\n"; } # to join all the edited elements will commas my $joined = join ",", @article; print $joined;

Update - Whoops!

Having solved the wrong problem here is a solution to the bibtex code parsing such as I understand it. Thanks to kschwab. Assuming a record as you have shown this does the trick.

my $string = <<'STRING'; @article{ author = {Author, J.P } , title = "A paper, about things" , etc.. } STRING my $open = ''; my $commaless = $1 if $string =~ s/^(\s*@\w+\s*{)//; for (split //,$string) { if (/{|"/ and not $open) { $open = /{/ ? '}' : '"'; $commaless .= $_; next; } $commaless .= $_ unless /,/ and $open; $open = '' if $open eq $_; } print $commaless;

First we eat up the opening @article{ so we get into the guts of the problem. Then the code splits the string into characters. Now, if we find an opening delimiter we set $open to the appropriate closing delimiter '}' or '"' depending on what it is and declare it open season on commas until we find the closing delimiter. We add all the chars which are not commas to $commaless and thus remove the commas as desired.

Trying to do this with a single regex would be difficult, and certainly harder to understand.

hope this helps

cheers

tachyon

s&&rsenoyhcatreve&&&s&n\w+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: Interesting Regex Question by tachyon
in thread Interesting Regex Question by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.