gpt4 book ai didi

java - Android:通过Regex方法传递文本,并显示在EditText上

转载 作者:行者123 更新时间:2023-12-01 10:33:37 25 4
gpt4 key购买 nike

我正在尝试将文本输出传递到正则表达式方法中,这样我就可以获取整个文本输出并提取电子邮件地址。

例如,我有以下文本输出:

Genos Taper
Assitant Manager

90606968
genostaper@hotmail.com

我想将上述文本输出传递到正则表达式方法中,并提取“genostaper@hotmail.com”并显示到 EditText 中。

下面是我的代码:

public class CreateContactActivityOCRtest extends Activity {

private String recognizedText, textToUse;
private EditText mEditText1, mEditText2;
private String mFromLang, mCurrentLang;

private Pattern pattern;
private Matcher matcher;

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 to show
Bundle extras = this.getIntent().getExtras();
recognizedText = extras.getString("TEXT");
textToUse = recognizedText;

// Getting the language used for text recognition
mFromLang = extras.getString("LANG");
mCurrentLang = mFromLang;
//Log.i(TAG, mFromLang);

textToUse = EmailValidator();
setupUI();
}

public String EmailValidator() {

String email = textToUse;

Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(email);

if (matcher.find()) {
return email.substring(matcher.start(), matcher.end());

} else {
// TODO handle condition when input doesn't have an email address
}

return email;

}

public boolean validate(final String hex) {

matcher = pattern.matcher(hex);
return matcher.matches();

}

public void setupUI(){

// Setting up the textbox

mEditText1 = (EditText)findViewById(R.id.EmailET);
mEditText2 = (EditText)findViewById(R.id.role);
mEditText1.setText(textToUse);
mEditText2.setText(textToUse);

}

}

我想做的是:

  1. 接收另一个类的文本输出(已完成)
  2. 将整个文本输出传递到 EmailValidator()(不确定我是否正确执行)
  3. EmailValidator() 获取输出并将其传递给 setupUI()(未完成)

哪里出了问题?文本输出的传递,还是正则表达式方法?有什么想法请提供。谢谢~~

最佳答案

您没有将返回值分配给textToUse

textToUse= EmailValidator();

关于java - Android:通过Regex方法传递文本,并显示在EditText上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34943273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com