use strict;
my $str = '### rgmz example
@----__-2as-2:password@----__-2as-2.com
### From test cases
var (
validPattern = " https://kaNydBSAodo87dSAFtsd7.com:1234@qYY3SylY7fHP "
validPattern2 = `<p><a href="http://username:password@127.0.0.1">http://username:password@127.0.0.1</a></p>`
invalidPattern = "https://kaNydBSAodo87dsm9asuiSAFtsd7.com.1234@qYY3SylY7fHP"
keyword = "uri"
)
### examples from chatgpt
1. `https://user.name:pa$$word123@sub.example.com`
2. `http://u$er_name:my%p@$$word@api.dev.localhost`
3. `https://12345:67890@my-app.server123.co.uk`
4. `http://user~!name:pass.word@some-long-subdomain.example.org`
5. `https://_:__@localhost`
6. `https://admin:admin123@192.168.1.1`
7. `http://test-user:complex!password#123@server.example.io`
8. `https://valid_user:!valid-password@simple-host.co`
9. `http://username_with.dots:password_with-dashes@domain.with.subdomain.net`
10. `https://username:password@127.0.0.1:8080`
11. `http://user:pass@----__-2as-2`
12. `https://user@localhost`
13. `http://user:pass@:invalidhost`
14. `https://:passwordonly@hostname.com`
15. `ftp://username:password@hostname.com`
16. `https://user:password@hostname_with_underscores.com`
17. `http://user@:missingpassword.com`
18. `https://missingcolonpassword@hostname.com`
19. `http://user:password@-invalid-hostname.com`
20. `https://user:password@host..com` ';
my $regex = qr`\b(?:https?:\/\/)?[\w-\.$~!&'()*+,;=:%-]{3,50}:([\w-\.%$^#&'()*+,;=:%-]{3,50})@[a-zA-Z0-9.-]+(?:\.[a-zA-Z]{2,})?\b`mp;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Perl, please visit: http://perldoc.perl.org/perlre.html