regex for single space and/or apostrophe in java

varun s

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]*\\')"
ctwheels

Code

Note: The link includes \r\n in the regex since the input is multiline

See regex in use here

^(?!(?:[^']*'){2})(?!(?:[^ ]* ){2})[a-z ']{2,}$

Results

Input

 'abc
' abc
abc '
abc' 
a 'bc
a' bc
abc
' 
ab

a
a'' bc
a  bc

Output

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

Explanation

  • ^ 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 line

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Position of Apostrophe in Java Regex

java regex to replace any single character with space

url encoding in java has issue with apostrophe and space

Regex to replace space with dash, spacedash, dotspace, dot and apostrophe with empty strings

Ignoring apostrophe while capturing contents in single quotes REGEX

Validate single dot and space in regex

RegEx for passing space and single quote

Bash regex not recognizing a single space " "

Java Regex for , not followed by a space

Regex: "AND OR" returns two groups, "ANDOR" returns no groups

Java replace issues with ' (apostrophe/single quote) and \ (backslash) together

In Java Regex - email validation, How to allow apostrophe before @ and not as first and last character before @ and not two consecutive apostrophe?

Java Regex: Split based on non-word characters except for apostrophe

Regex to replace multiple spaces with a single space

PowerShell regex split on single and double white space

Regex matching every single blank space

Text substitution with single apostrophe

Appropriate RegEx for A-Za-z0-9 apostrophe, space, and hyphen with range

Capturing apostrophe using regex

Escaping apostrophe in regex?

Ignoring Apostrophe in Regex

add apostrophe to regex match

Regex word search with apostrophe

Regex for allowing apostrophe and period

Regex allowing a space character in Java

Java regex: newline + white space

How can we select string between two single quotes by escaping apostrophe using regex in javascript?

how to replace apostrophe (’) with single quote(')

Apostrophe (single quote) in connection string

TOP Ranking

HotTag

Archive