Positive outlook on exclamation mark
I am trying to create a Java regex to match " .jar!
"
The catch is that I don't want the helper to use an exclamation mark. I tried using Pattern.compile("\\.jar(?=!)")
it but it failed. As well as running over an exclamation point.
Can anyone get this to work or is this a JDK bug?
UPDATE : I feel like an idiot, it Pattern.compile("\\.jar(?=!)")
works. I have used Matcher.matches () instead of Matcher.find ().
+1
source to share
2 answers
Using your regex works for me (using Sun JDK 1.6.0_02 for Linux):
import java.util.regex.*;
public class Regex {
private static final String text = ".jar!";
private static final String regex = "\\.jar(?=!)";
public static void main(String[] args) {
Pattern pat = Pattern.compile(regex, Pattern.DOTALL);
Matcher matcher = pat.matcher(text);
if (matcher.find()) {
System.out.println("Match: " + matcher.group());
} else {
System.out.println("No match.");
}
}
}
prints:
Match: .jar
(without!)
+1
source to share
Alternatively, you can try boxing it
Pattern.compile("\\.jar(?=[!])")
Java must be broken: Perl
use strict;
use warnings;
my @data = qw( .jar .jar! .jarx .jarx! );
my @patterns = (
"\\.jar(?=!)",
"\\.jar(?=\\!)",
"\\.jar(?=[!])",
);
for my $pat ( @patterns ){
for my $inp ( @data ) {
if ( $inp =~ /$pat/ ) {
print "$inp =~ $pat \n";
}
}
}
->
.jar! =~ \.jar(?=!) .jar! =~ \.jar(?=\!) .jar! =~ \.jar(?=[!])
sub>
0
source to share