Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 710145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:34:57+00:00 2026-05-14T04:34:57+00:00

I need the Perl regex to parse plain text input and convert all links

  • 0

I need the Perl regex to parse plain text input and convert all links to valid HTML HREF links. I’ve tried 10 different versions I found on the web but none of them seen to work correctly. I also tested other solutions posted on StackOverflow, none of which seem to work. The correct solution should be able to find any URL in the plain text input and convert it to:

<a href="$1">$1</a>

Some cases other regular expressions I tried didn’t handle correctly include:

  1. URLs at the end of a line which are followed by returns
  2. URLs that included question marks
  3. URLs that start with ‘https’

I’m hoping that another Perl guy out there will already have a regular expression they are using for this that they can share. Thanks in advance for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:34:58+00:00Added an answer on May 14, 2026 at 4:34 am

    When I tried URI::Find::Schemeless with the following text:

    Here is a URL  and one bare URL with 
    https: https://www.example.com and another with a query
    http://example.org/?test=one&another=2 and another with parentheses
    http://example.org/(9.3)
    
    Another one that appears in quotation marks "http://www.example.net/s=1;q=5"
    etc. A link to an ftp site: ftp://user@example.org/test/me
    How about one without a protocol www.example.com?

    it messed up http://example.org/(9.3). So, I came up with the following with the help of Regexp::Common:

    #!/usr/bin/perl
    
    use strict; use warnings;
    use CGI 'escapeHTML';
    use Regexp::Common qw/URI/;
    use URI::Find::Schemeless;
    
    my $heuristic = URI::Find::Schemeless->schemeless_uri_re;
    
    my $pattern = qr{
        $RE{URI}{HTTP}{-scheme=>'https?'} |
        $RE{URI}{FTP} |
        $heuristic
    }x;
    
    local $/ = '';
    
    while ( my $par = <DATA> ) {
        chomp $par;
        $par =~ s/</&lt;/g;
        $par =~ s/( $pattern ) / linkify($1) /gex;
        print "<p>$par</p>\n";
    }
    
    sub linkify {
        my ($str) = @_;
        $str = "http://$str" unless $str =~ /^[fh]t(?:p|tp)/;
        $str = escapeHTML($str);
        sprintf q|<a href="%s">%s</a>|, ($str) x 2;
    }
    

    This worked for the input shown. Of course, life is never that easy as you can see by trying (http://example.org/(9.3)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.