您现在的位置是:首页 > 电脑 > 

\\ p {Punct}在我的android应用程序中的行为(\\p{Punct} behavior in my android app)

2025-07-13 21:03:42
\\ p {Punct}在我的android应用程序中的行为(\\p{Punct} behavior in my android app) 这是我的正则表达式 - “[\\w\\d\\p{Punct}]+” 在我的应用程序中\\p{Punct}是非常奇怪的行为。 根据文档( https://docs.oracle.com/javase/7/docs/api
\\ p {Punct}在我的android应用程序中的行为(\\p{Punct} behavior in my android app)

这是我的正则表达式 - “[\\w\\d\\p{Punct}]+”

在我的应用程序中\\p{Punct}是非常奇怪的行为。 根据文档( https:///javase/7/docs/api/java/util/regex/Pattern.html ):

\ p {Punct} - 标点符号:!“#$%&'()* +, - 。/ :; <=>?@ [] ^ _`{|}〜

但在我的应用程序中,此标记始终忽略此字符:“$”,“+”,“<”,“>”,“=”,“^”,“`”,“|”,“〜”

abc!d => true abc#d => true abd$d => false abc<>d = > false

等等

我试着用

Pattern pattern = ("[\w\d\p{Punct}]+", Pattern.UICODE_CASE);

没有效果。

Anny建议我做错了什么?

PS:我为它使用TextWatcher。 我这样做:

TextWatcher textWatcher = new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // I am checking it here // if if(!().matches("[\\w\\d\\p{Punct}]+")) => do something } @Override public void afterTextChanged(Editable s) { } 

};

当我将这个TextWatcher添加到MyEditText :

myEditText.addTextChangedListener(textWatcher);

This is my regex – “[\\w\\d\\p{Punct}]+”

It is very strange behavior of \\p{Punct} in my app. According to the docs (https:///javase/7/docs/api/java/util/regex/Pattern.html):

\p{Punct} - Punctuation: One of !"#$%&'()*+,-./:;<=>?@[]^_`{|}~

But in my app this tag always omits this characters: “$”, “+”, “<”, “>”, “=”, “^”, “`”, ”|”, ”~”

abc!d => true abc#d => true abd$d => false abc<>d = > false

etc.

I tried to use

Pattern pattern = ("[\w\d\p{Punct}]+", Pattern.UICODE_CASE);

There was no an effect.

Anny suggesti what I am doing wrong?

P.S.: I use TextWatcher for it. I do it like this:

TextWatcher textWatcher = new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // I am checking it here // if if(!().matches("[\\w\\d\\p{Punct}]+")) => do something } @Override public void afterTextChanged(Editable s) { } 

};

When I added this TextWatcher to MyEditText:

myEditText.addTextChangedListener(textWatcher);

最满意答案

问题是\p{Punct}与POSIX [:punct:]字符类符号相匹配,该符号还包含符号\p{S}

为避免匹配没有符号的字符串,您需要

"^[\\w\\p{P}]+$"

另外,请注意,在Android上,默认情况下, \w , \p{P}和\p{S}是Unicode识别的。

另外, \w已经匹配数字,没有意义将\w和\d到同一个字符类[...] 。

The issue is that \p{Punct} matches the POSIX [:punct:] character class symbols that also include symbols, \p{S}.

To avoid matching the strings without symbols you need:

"^[\\w\\p{P}]+$"

Also, note that on Android, \w, \p{P} and \p{S} are Unicode-aware by default.

Besdides, \w already matches digits, there is no point adding both \w and \d into the same character class [...].

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/61876.html

相关标签:无
上传时间: 2023-04-21 00:31:41
留言与评论(共有 7 条评论)
本站网友 小茴香苗
13分钟前 发表
int count) { // I am checking it here // if if(!().matches("[\\w\\d\\p{Punct}]+")) => do something } @Override public void afterTextChanged(Editable s) { } 

}; 当我将这个TextWatcher添加到MyEditText : myEditText.addTextChangedListener(textWatcher); This is my regex – “[\\w\\d\\p{Punct}]+” It is very strange behavior of \\p{Punct} in my app. According to the docs (https
本站网友 vagaa绿色版
4分钟前 发表
根据文档( https
本站网友 珍菊降压片
29分钟前 发表
我这样做: TextWatcher textWatcher = new TextWatcher() { @Override public void beforeTextChanged(CharSequence s
本站网友 6666是什么意思
28分钟前 发表
punct
本站网友 普京演讲
10分钟前 发表
\w已经匹配数字
本站网友 租房中介费怎么算
8分钟前 发表
\p{P} and \p{S} are Unicode-aware by default. Besdides