I'm having issues with my regex.Could anyone please help me on this? Requirement: String should be alphabetic and can include single apostrophe and or single space(size should be minimum of 2)
Valid strings:
1. 'abc
2.' abc
3.abc '
4.abc'
5.a 'bc
6.a' bc
I have used the below regex.It works for scenario 2,4,6 but doesn't work for scenario 1,3,5
Regex:
"(([a-zA-Z][a-zA-Z])| " +
"([a-zA-Z]*\\s\\'[a-zA-Z]*)|" +
"([a-zA-Z]*\\'\\s[a-zA-Z]*)|"+
"[a-zA-Z]*|" +
"[a-zA-Z]\\s|" +
"[a-zA-Z]\\'|" +
"\\s[a-zA-Z]|" +
"\\'[a-zA-Z]|"+
"\\s[a-zA-Z]*|"+
"\\'[a-zA-Z]*|" +
"[a-zA-Z]*\\s|"+
"[a-zA-Z]*\\')"
Note: The link includes \r\n
in the regex since the input is multiline
^(?!(?:[^']*'){2})(?!(?:[^ ]* ){2})[a-z ']{2,}$
'abc
' abc
abc '
abc'
a 'bc
a' bc
abc
'
ab
a
a'' bc
a bc
Below are only strings that match requirements.
Note: The second to last string sample is '
(apostrophe and space), which, according to the OP's requirements, should match.
'abc
' abc
abc '
abc'
a 'bc
a' bc
abc
'
ab
^
Assert position at the start of the line(?!(?:[^']*'){2})
Negative lookahead ensuring what follows doesn't include 2 apostraphes '
(?!(?:[^ ]* ){2})
Negative lookahead ensuring what follows doesn't include 2 spaces
[a-z ']{2,}
Match two or more of the characters in the set$
Assert position at the end of the lineCollected from the Internet
Please contact [email protected] to delete if infringement.
Comments