M
M
Mikhail Konyukhov2012-06-21 19:00:59
Java
Mikhail Konyukhov, 2012-06-21 19:00:59

How to search in a string by Russian pattern in Java?

I immediately apologize for the trivial question, I am new to Java. There is a set of strings like this:
Arctikot|data.28.xml|3029
Arktogeya|data.84.xml|3898
Arlov|data.90.xml|19
Armavir|data.23.xml|
628 there definitely is:
cat|data.1.xml|4132
I'm looking for this:

while ((str = in.readLine()) != null) {
   //keyword - текст "кот"
   if (str.matches("^"+keyword+"\\|")){
     ....
   }
}

in the end it doesn’t find it ... There is a suspicion that the problem is that the regexp contains Russian characters. How to be?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
B
barker, 2012-06-21
@piromanlynx

str.matches is the same as Pattern.compile(regexp).matcher(str).matches()
In your case, as you rightly said above, you need to find. In general, str.startsWith("cat") is also suitable for you.

M
Moxa, 2012-06-21
@Moxa

        String s = "кот|data.1.xml|4132";
        Pattern pattern = Pattern.compile("^" + "кот" + "\\|");
        Matcher matcher = pattern.matcher(s);
        System.out.println(matcher.find());

L
leventov, 2012-06-21
@leventov

String.matches returns true if the string is completely described by the regular expression. "cat|" does not match "cat|data.1.xml|4132". Add an asterisk at the end:

str.matches("^кот\\|*")

B
BrightGenie, 2012-06-22
@BrightGenie

There are some inconveniences in Java when working with regular expressions and text containing Unicode (read Cyrillic).
read here for more details: stackoverflow.com/questions/4304928/unicode-equivalents-for-w-and-b-in-java-regular-expressions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question