I'm trying to write a regex that tests a single sentence. The sentence can contain any content and should either: end in a period and have nothing following that period or not have a period or any ending punctuation.
I started with this: .*?\.$ and it worked fine testing for a sentence ending in a period. But if I mark the period as optional .*?\.?$ then a sentence can have any ending including a period and text after that period.
To be clear, these should pass the test: He jumped over the fence. He jumped over the fence
And this should not pass the test: He jumped over the fence. She jumped over it too.
CodePudding user response:
Try:
^(?:[^.] \.|[^.] )$
^ - start of the string
(?:[^.] \.|[^.] ) - match either [^.] \. (one or more non-. characters and .) or [^.] (one or more non-. characters) in non-capturing group.
$ - end of the string
CodePudding user response:
You can use such regex:
.*?[^.]$
Optional (?) means that regex will match if symbol presents or not presents in string
[^.]$ - means that you want to exclude the presence of a dot at the end of a sentence.
CodePudding user response:
This pattern .*?\.$ can match the whole line He jumped over the fence. She jumped over it too. because the . can also match a literal dot.
If you don't want to cross newlines and you do want to match for example 1.2m when having to end on a dot, or matching only chars other than ending punctuations:
If a lookahead assertion is supported:
^(?:[^\.\n]*(?:\.(?![^\S\n])[^\.\n]*)*\.|[^!?.\n] )$
Explanation
^Start of string(?:Non capture group[^\.\n]*Match optional chars other than a dot(?:\.(?![^\S\n])[^\.\n]*)*Optionally repeat matching a dot not directly followed by a space\.Match a dot|Or[^!?.\n]Match 1 times any char except for!?.or a newline (Or add more ending punctuation chars)
)Close the non capture group$End of string
See a regex101 demo
