When I put b on Pattern.Compiler it returns find as false

Asked

Viewed 72 times

1

When I put b in pattern.compiler he returns the matcher.find as false, because he can’t even find a pattern precisely because of the b.

Following the code I use:

final Pattern py = Pattern.compile("\\b(print|True|False|int|str|float|bool)\\b)");

edittext.addTextChangedListener(new TextWatcher() {
    Matcher m;
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    } 
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    } 
    @Override 
    public void afterTextChanged(Editable e) {
        m = py.matcher(e);
        while (m.find()) {
            e.setSpan(new ForegroundColorSpan(Color.WHITE),m.start() ,m.end() ,0);
        }
    }
});

But if I take out the b works but not the way I want.

  • What are you testing for? Pattern doesn’t give us a hint of the real problem

  • I don’t have a PC and I compile java in app by Sketchware. It is an IDE

2 answers

2

Thank you @Paz for your reply but simply I use an app that uses bars in its string: Ex:

String A = "\\b";

What was actually happening was that the app took one bar of java another and only got the b with the default string knowing that I put another bar ex:

String  a = "\\\\b";

It was an inattention of mine.

1


I believe you’re using the matcher.find incorrectly, the value returned by this method changes at each position of the parsed string.

Your pattern is giving match and finding the word print, but it still analyzes the last position of the text (position marked by the token $ in that example) and not the match, changing the value of matcher to false.

So for your case the best way is to use it as a condition for a while or if change a boolean to true if found and use this value as a condition.

import java.util.regex.*;  
public class RegexExampleMatcher{  
public static void main(String args[]){  
  boolean achou = false;
  String content = "print";                          //String analisada                    
  Pattern pattern = Pattern.compile("\\bprint\\b");  //String que será usada como padrão
  Matcher matcher = pattern.matcher(content);        //Matcher para usar o método find

  while(matcher.find()) {                            //Enquanto matcher.find for true
     System.out.println("Achou");                    //Imprima achou
     achou = true;                                   //muda o boolean achou para true
  }
  if (achou){
     //método que será executado
     }
}
}

You can test this example here

  • But it turns out that find is false ,i.e., it does not execute either a block or the if nor the while.

  • Yes I am, entrance and a EditText and the exit should be on itself EditText with one of the words highlighted, but by the Matcher not finding even a pattern it does not highlight those words.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.