regex - What's wrong with this particular regular expression? -


i need write regular expression validate input in form. want restrict use of these characters: \ / & < > " . else allowed.

examples of valid inputs are: my basket, groceries, fruits, £$%, , +=.

examples of invalid inputs are: a&b, a > b, 2 / 3, , a<>c.

below code i'm using not working properly, because returns valid inputs invalids.

public class main {      public static void main(string[] args) throws ioexception {          bufferedreader br = new bufferedreader(new inputstreamreader(system.in));          while (true) {             system.out.print("\nenter text: ");             string inputtext = br.readline();              system.out.println("\nthe input " + (isvalidinput(inputtext) ? "valid" : "invalid"));         }     }      public static boolean isvalidinput(string inputtext) {          pattern p = pattern.compile("[^/\\\\/&<>\"]");         matcher matcher = p.matcher(inputtext);          return matcher.find();     } } 

finding [^/\\\\&<>\"] check @ least 1 of character isn't forbidden one.

if want check the whole string made of allowed characters, have anchor regex:

pattern.compile("^[^/\\\\&<>\"]*$").matcher(inputtext).find(); 

with ^$ matching beginning , end of string.

or, pointed out @devnull, can use string.matches wihch anchors regex default:

inputtext.matches("[^/\\\\&<>\"]*") 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -