[英]Using Android Pattern and Matcher class (Regex)
我剛剛選擇了 Android,但我的任務是幫助我完成一個實習項目。
可以說我有以下詳細信息:
Fonia Taylo
Product Manager
foniataylo@gmail.com
98706886
根據我上面的詳細信息,我想將它傳遞到一個類中,然后我可以使用正則表達式過濾掉電子郵件地址,並且只將這個過濾掉的電子郵件地址傳遞給 EditText。
我搜索了很多關於正則表達式的教程,尤其是關於 Android Pattern 和 Matcher 類的教程。
但我發現的所有示例僅用於驗證輸入到 EditText 字段中的文本。
我需要做的是:
目前下面是我的課:
public class RegexOCR1 extends Activity {
private Pattern pattern;
private Matcher matcher;
private String recognizedText, textToUse;
private static final String EMAIL_PATTERN =
"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_createcontact);
// Getting the path of the image from another class
Bundle extras = this.getIntent().getExtras();
recognizedText = extras.getString("TEXT");
textToUse = recognizedText;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.usetext);
showText();
//Log.i(TAG, "onConfigChanged");
}
private void showText(){
//Log.i(TAG, "ShowText");
Intent intent = new Intent(this, CreateContactActivityOCR.class);
startActivity(intent);
}
public EmailValidator() {
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(textToUse);
if (matcher.find())
{
String email = textToUse.substring(matcher.start(), matcher.end());
} else {
// TODO handle condition when input doesn't have an email address
}
}
public boolean validate(final String hex) {
matcher = pattern.matcher(hex);
return matcher.matches();
}
}
如您所見,它幾乎不完整。 我想將“textToUse”傳遞給正則表達式驗證,然后繼續執行上述功能。
編輯:
經過以下方法:
public EmailValidator() {
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(textToUse);
if (matcher.find())
{
String email = textToUse.substring(matcher.start(), matcher.end());
} else {
// TODO handle condition when input doesn't have an email address
}
}
提取電子郵件地址; 然后我如何將這個提取的電子郵件地址通過意圖傳遞給另一個 Class中的EditText ?
如果有任何想法,請告訴我如何更改我的代碼。 謝謝!
下面是一些提取與模式匹配的文本的代碼:
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(inputString);
if (matcher.find()) {
String email = inputString.substring(matcher.start(), matcher.end());
} else {
// TODO handle condition when input doesn't have an email address
}
在 Android SDK 中,有一個名為android.util.Patterns
的類,您可以在其中找到一些有用的正則表達式模式。
電子郵件地址:
android.util.Patterns.EMAIL_ADDRESS
您可以像這樣簡單地使用它們:
String target = "";
if (android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches()) {
// valid email address
}
只需首先通過創建一個單獨的類將 Web 內容下載到您的 android studio 日志中,如下所示:
公共類 MainActivity 擴展 AppCompatActivity {
public class DownloadTsk extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while (data != -1) {
char current = (char) data;
result += current;
data = reader.read();
}
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
然后在 onCreate 方法中使用此類並下載內容 ==> 在 Log 中對其進行測試,最后使用 Pattern 和 Matcher,如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DownloadTsk task = new DownloadTsk();
String result = null;
try {
result = task.execute("YOUR WEBSITE ADDRESS..... ").get();
Log.i("Content", result);
} catch (Exception e) {
e.printStackTrace();
}
Pattern p = Pattern.compile("WHTEEVER BEFORE(.*?)WHTEEVER AFTER ");
Matcher m = p.matcher(result);
while (m.find()) {
Log.i("result", m.group(1)) ;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.