java - Regex to check for all letters in the alphabet -
i'm brushing on coding skills , working through easy problems i've found online. particular task input txt file contains number of lines, , have program check each line , return "true" or "false" depending on whether line contains 26 letters of alphabet. feel i'm finished, regular expression match string [a-z] returns false no matter do. i've tried changing string lowercase, removing spaces, , nothing seems work.
the text have in text file "the quick brown fox jumps on lazy dog."
package easy139; import java.io.filereader; import java.io.ioexception; import java.util.scanner; import java.util.regex.pattern; import java.util.regex.matcher; public class easy139 { public static void main(string[] args) { try { scanner in = new scanner(new filereader("input.txt")); while (in.hasnextline()) { string line = in.nextline(); system.out.println(line); string nospaces = line.replaceall(" ",""); if (nospaces.matches("[a-z]")) { system.out.println("true"); } else { system.out.println("false"); } } in.close(); } catch (ioexception e) { } } }
your test returning false because regex [a-z] means "exactly 1 letter".
a regex works string.matches() is:
(?i)(?=.*a)(?=.*b)(?=.*c)...(?=.*z).* this uses 1 ahead each letter, each of asserts letter present. (?i) switch turns on case insensitivity.
Comments
Post a Comment