简体   繁体   中英

Need help in regular expression in java

Can somebody help me how to change the below regular expression in such a way that it doesn't allow hyphen and Apostrophe in the first and/or last position. Any help is appreciated.

"[a-zA-Z][\\s-'a-zA-Z]{0,14}"
"[a-zA-Z][\\s'a-zA-Z-]{0,14}(?<!['-])"

(?<!['-])否定式的隐式断言 ,要求其前面的字符不匹配['-]

[a-zA-Z][\\\\s-'a-zA-Z]{0,14}(?<!['-])

Java regex patterns: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html

Perhaps something like this, my regex is not the best and may need to be corrected.

public bool checkStringForHorA(String s){
 s.matches("\\^(-'\\).\\^(-'\\)") ? return true: return false;
}

The regex should check to see if it starts with a - or ', or if after 0 to many characters . it ends with ' or -. If it does then it will return a true, if it does not then it will return a false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM