1078081
1078081

Reputation: 141

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}"

Upvotes: 0

Views: 96

Answers (3)

Sam Greenhalgh
Sam Greenhalgh

Reputation: 6136

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

Upvotes: 1

Amber
Amber

Reputation: 526573

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

(?<!['-]) is a negative lookbehind assertion that requires that the character which precedes it not match ['-].

Upvotes: 2

Travis J
Travis J

Reputation: 82267

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.

Upvotes: 0

Related Questions