Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class MainActivity extends AppCompatActivity {
- private static final String TAG="REGEX";
- private final String REGEX="\\boleg.\\b";
- private final String INPUT="oleg oleg2 olegy olega aleg aoleg";
- private final String REPLEACE = "Bar ";
- private final String REGEX_OLEG="Oleg";
- private final String INPUT2="Oleg bought a new Volvo and Oleg bought a villa and a viola";
- private final String REPLEACE_TO_TRUTH="Anna";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //regex1();
- regex2();
- //regex3();
- }
- private void regex1() //find
- {
- Pattern p = Pattern.compile(REGEX);
- Matcher m= p.matcher(INPUT);
- int counter=0;
- while (m.find())
- {
- counter+=1;
- }
- Log.e(TAG, "regex1: oleg was found "+counter+" times");
- }
- private void regex2() //replace
- {
- Pattern p = Pattern.compile(REGEX);
- Matcher m = p.matcher(INPUT);
- String input = m.replaceAll(REPLEACE);
- Log.e(TAG, "regex2: "+input );
- }
- private void regex3()
- {
- Pattern p = Pattern.compile(REGEX_OLEG);
- Matcher m = p.matcher(INPUT2);
- int counter=0;
- while (m.find())
- {
- counter+=1;
- }
- Log.e(TAG, "regex3: need to replace oleg "+counter+" Times");
- String theTruth=m.replaceAll(REPLEACE_TO_TRUTH);
- Log.e(TAG, "regex3: "+theTruth);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement