Reputation: 7792
This is a general question. I'd like to know do they behave differently and why, or do they behave differently only when we do something wrong ?
This is what I'm currently struggling with. I have this regex:
CLASS_NAME_VALIDATION_REGEX = "([a-zA-Z_$][a-zA-Z\\d_$]*\\.)*[a-zA-Z_$][a-zA-Z\\d_$]*";
and I use it like this:
Pattern.matches(CLASS_NAME_VALIDATION_REGEX, qualifiedClassName)
So under Unix/Linux matches()
returns true for java.io.Serializable
on Windows it returs false.
Have I escaped something improperly or is there something else I'm not aware of ?
Thanks,
So its probably not the regex that is the problem and I'm thinking of closing this question since it would be very much off topic if I'm right.
I'm currently reading a file containing class names and matching each name with the regex. Each name is on a separate line.
Could it be a case of different characters for carriage return on Windows and Linux ?
Upvotes: 2
Views: 3607
Reputation: 7792
So it turns out the problem is not the regex as commenters pointed out.
For anyone finding this question java regexes do not work differently on Windows and Linux.
The actual problem was that on Linux lines end with \n
while on Windows they end with \r\n
and as ajb suggested I had a leftover \r
at the end of each class name.
So
Pattern.matches("([a-zA-Z_$][a-zA-Z\\d_$]*\\.)*[a-zA-Z_$][a-zA-Z\\d_$]*", "java.io.Serializable\r")
returned false
.
Upvotes: 4
Reputation: 86774
Works for me
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest
{
private static final Pattern pat = Pattern.compile("([a-zA-Z_$][a-zA-Z\\d_$]*\\.)*[a-zA-Z_$][a-zA-Z\\d_$]*");
public static void main(String[] args) throws IOException
{
String data = "java.io.Serializable";
Matcher m = pat.matcher(data);
System.out.println(m.matches());
}
}
Output:
true
Upvotes: 2