Check character to real-time character String by regular expression

0

I would like that while I write characters it should be checked if the new String complies with what in the regular expression is defined.

I can say that the Strign is entering it in a TextField

this is the regular expression - > "[0-9] {8} [A-Z]"

this the code of the event:

  public RestrictiveField(){

   textProperty().addListener(new ChangeListener<String>(){
        @Override
        public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {

         //patron.get() es el metodo que carga el patron 
        Pattern p= Pattern.compile(patron.get());
          Matcher m= p.matcher(newValue);
          if(!m.lookingAt()){

                setStyle("-fx-control-inner-background: red; -fx-font-size: 16;");
                   } 
            if(newValue.equals("")|| m.lookingAt()){

                setStyle("fx-control-inner-background: transparent;");

            }
          }  
      });

}

The output of this, is just the opposite of what I'm looking for with the code I have so far

to see if someone can help me.

    
asked by Miguel Méndez Liébana 05.12.2018 в 20:07
source

2 answers

1

For partial results, try the following regular expression:

^(?:\d{8}[A-Z]|\d{1,8})$

You have a demo here .

Then, once the form is submitted, validate with the original regular expression

^[0-9]{8}[A-Z]$

    
answered by 07.12.2018 в 11:05
0

I appreciate the answer but (maybe you explain me wrongly) but it does not answer my problem.

The problem is not the regular expression but the methods to use. I recently solved it:

 public RestrictiveField(){

textProperty (). addListener (new ChangeListener () {         @Override         public void changed (ObservableValue observable, String oldValue, String newValue) {

     //patron.get() es el metodo que carga el patron 
    Pattern p= Pattern.compile(patron.get());
      Matcher m= p.matcher(newValue);
      m.matches();
      if(!m.hitEnd()()){

            setStyle("-fx-control-inner-background: red; -fx-font-size: 16;");
               } 



        if(newValue.equals("")|| m.hitEnd()){

            setStyle("fx-control-inner-background: transparent;");

        }
      }  
  });

}

This way you will progressively check if the entered (every new String) matches the pattern

greetings

    
answered by 09.12.2018 в 11:58