You could replace the first line with a tr///cd, that should be a bit faster. The second line is the usual way to trim whitespace from a string in Perl so it's fine the way it is.

However, "" is the Byte order mark when the file is encoded in UTF-8 but was opened with the incorrect encoding. So instead of that first regex, you probably want to open the file with open my $fh, '<:raw:encoding(UTF-8)', $filename or die "$filename: $!";, and then do a $line =~ s/\A\N{U+FEFF}//; on the first line of the file. This has the major advantage that any other UTF-8 encoded characters in the file will be decoded correctly - meaning you won't get "strange characters", you'll get the correct Unicode characters, assuming no other encoding issues - and this really is the correct way to solve this issue. If you then still want to turn the text into ASCII-only, see e.g. Text::Unidecode.

Updated: A few edits for clarification. Also: If you have further issues with encoding, I have some brief advice on what to post to get the best answers here.


In reply to Re: Is there a way to make these two regex lines cleaner? by haukex
in thread Is there a way to make these two regex lines cleaner? by bartender1382

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.