喜不工作,我想通過字符匹配名字字符,但這個做一個錯誤,這是我的代碼:正則表達式的Android
int length = input.length();
for (int i = 0; i < length; i++){
char ch = input.charAt(i);
String regex ="^[_\\s]||[آ-ی]$";
Matcher matcher = Pattern.compile(regex).matcher(ch);
,這是我的完整代碼:
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
String input;
String result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.edt);
button = (Button)findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@SuppressLint("ShowToast")
@Override
public void onClick(View v) {
input = editText.getText().toString();
check_reg();
}
});
}
public boolean check_reg(){
int length = input.length();
for (int i = 0; i < length; i++){
char ch = input.charAt(i);
String regex ="^[_\\s]||[آ-ی]$";
Matcher matcher = Pattern.compile(regex).matcher(ch);
if (matcher.find())
{
result = matcher.group();
Toast.makeText(MainActivity.this, "match", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this, "no match", Toast.LENGTH_SHORT).show();
}
}
return false;
}
}
,這是我的問題的一個形象:
我猜它的Java的東西? –
從我在[docs](https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html)中讀到的內容中,'pattern.matcher()'方法只接受'String'和'CharSequence'類型...不是'char' –
爲什麼要按字符檢查字符?你可以使用'^ [_ \ sā-ı] + $'來檢查它作爲一個整個字符串。 'input.matches(「[_ \ sā-ı] +」)' –