Android正则表达式大全收集:
验证邮箱:
1
2
3
4
5
6
|
public
static
boolean
isEmail(String strEmail) {
String strPattern =
"^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$"
;
Pattern p = Pattern.compile(strPattern);
Matcher m = p.matcher(strEmail);
return
m.matches();
}
|
检查EditText中输入的是否符合规则:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import
Android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
public
class
Main
extends
Activity {
private
EditText editText;
private
Button button;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) findViewById(R.id.textId);
editText.setText(
"EditText element"
);
button = (Button) findViewById(R.id.btnId);
button.setText(
"Check"
);
button.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
if
(checkString(editText.getText().toString())) {
editText.setText(
"Corect"
);
}
}
});
}
private
boolean
checkString(String s) {
return
s.matches(
"\\w*[.](Java|cpp|class)"
);
}
}
|