use strict;
my $str = 'cat
cut
cot
c@t$
c?t
c t?
caut.
caat
cuut!
hello
hello world
lite.ral $ sign
lit\\erbl ^ car[a]t
Robert
Bob
Rob
Rick
Rick
lo
lol
loll
lolll
lollll
lolllll
lollllll
lolllllll
lollllllll
lolllllllll
lollllllllll
lolllllllllll
lollllllllllll
while (a < b) { while (c < d) { --d;}; while (b < c) { ++a; --c; } }
10001
18052
19146
<b>thing</a>
+1 555-234-1234 == matches
(654) 999-0234 == matches
+1 (101) 234 9838 == matches
333 444.5555 == matches
111.234-3463 == matches
+1-333-333-3333 == doesn\'t match (drops the +1)
555 2341234 == doesn\'t match (missing a separator)
231-1115 == doesn\'t match (missing an area code)
doowopdoo
shoobeedoobee
doodoodoo
doowop
0
C
a@b.c
bob@bob.bob
steve@steve.co.uk
tom.jones@its.not.unusual
0.0
+.03
-100.3
4.5e5
+0.03e-9
.01e12
.e4
-.e
public static void main(String[] args) {
return;
}
private double thingDoer (int thingA , float thingB) {
while (thingA < thingB) { ++thingA; }
return thingA * thingB;
}
boolean blobber(){
// do blob things
return false;
}
invalidMethodSignature here{
return;
}
anotherInvalidOne(){ return; }';
my $regex = qr/(?:(?:(public|protected|private)\s+)|(?:(abstract|static)\s+)|(?:(final)\s+)|(?:(volatile|synchronized)\s+)|(?:(native|strictfp)\s+))*([a-zA-Z_][[:alnum:]]+)\s+([a-zA-Z_][[:word:]<>\[\]]+)\s*\(\s*(?:(?:([a-zA-Z_][[:word:]<>\[\]]+)\s+([a-zA-Z_][[:alnum:]]+)\s*)(?:,\s*([a-zA-Z_][[:word:]<>\[\]]+)\s+([a-zA-Z_][[:alnum:]]+)\s*)*)?\)\s*\{/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