开发者

Regexp Pattern.MULTILINE problem

开发者 https://www.devze.com 2023-02-18 03:14 出处:网络
I don\'t understand why if I use: boolean found = Pattern.compile(\"^\\\\d\", Patter开发者_Python百科n.MULTILINE).matcher(\"dfg\\n5t\").find();

I don't understand why if I use:

boolean found = Pattern.compile("^\\d", Patter开发者_Python百科n.MULTILINE).matcher("dfg\n5t").find();

, it returns true.

But If I use:

// taken from two input field with the same above values!!!
String rx = txt_rx.getText();
String ch = txt_ch.getText();

boolean found = Pattern.compile(rx, Pattern.MULTILINE).matcher(ch).find();

, it returns false.

Thanks.


You say that strings returned by getText() are the same as string literals, but they shouldn't be the same!

\\ and \n are special escape sequences which are interpreted (as \ and newline respectively) in string literals only. If you want to read the same strings as you get after interpretation of string literals from the text fields, you should enter them as ^\d and

dfg
5t

respectively. You need a mulitline text field to enter the latter value (JTextArea in Swing).

0

精彩评论

暂无评论...
验证码 换一张
取 消