Reputation: 63
I have just picked up Android, but am tasked to help with a project for my internship.
Lets say I have the details below:
Fonia Taylo
Product Manager
[email protected]
98706886
From the details I have above, I want to pass it into a class whereby I can then filter out the email address using regex, and pass only this filtered out email address to an EditText.
I have searched many tutorials on regex, especially on Android Pattern and Matcher classes.
But all the examples I have found are only for validation for the text entered into an EditText field only.
What I need to do is:
Currently below is my class:
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();
}
}
As you can see, it is pretty much incomplete. I would like to pass "textToUse" into the regex validation, and then continue to perform the function as stated above.
Edit:
After the following method:
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
}
}
which extract out the email address; How do I then pass this extracted email address to through intent to an EditText that is in another Class?
Please let me know you how I can change my code if there are any ideas. Thank you!
Upvotes: 4
Views: 21170
Reputation: 29285
In Android SDK, there is a class called android.util.Patterns
, in which you can find some useful regex patterns.
E-Mail Address:
android.util.Patterns.EMAIL_ADDRESS
You can simply use them like this:
String target = "";
if (android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches()) {
// valid email address
}
Upvotes: 2
Reputation: 138
Simply first you download the Web content to your android studio log by creating a separated class as following :
public class MainActivity extends 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;
}
}
Then you use this class in onCreate method and download the content ==> test it in Log and Finally you use Pattern and Matcher like following:
@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)) ;
}
}
Upvotes: 0
Reputation: 30985
Here is some code to extract the text matching the pattern:
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
}
Upvotes: 5