Reputation: 1771
I know this should probably be easier than I am making this. Sorry if this is an easy question.
I am using regular expressions for my Rails application (do regular expressions change based on the language?) and I want to take a phrase like this: "I am not familiar with RegExp. Hopefully SO will help me out here. This is a third sentence which should not be included." and have RegExp return "Hopefully SO will help me out here" (without the period and whitespace at the beginning and without the period at the end)
Here is what I have tried so far:
^([^(.)\s]+)
([^\.\s]+)
((\.\s))
\.\s+\K.+
I really don't know what to do here. Thanks again for your help!
Upvotes: 0
Views: 354
Reputation: 626748
You may use
s[/\.\s+([^.]+)\./, 1]
See the Ruby demo, output: Hopefully SO will help me out here
.
Here, the [/regex/, 1]
syntax means you want to apply the regex
to the s
string and only output the contents of Group 1. Thus, you do not really need a \K
match reset operator, but surely you may use
s[/\.\s+\K[^.]+(?=\.)/]
that will yield the same result.
Upvotes: 1
Reputation: 1771
Thanks to the comments that enabled me to solve this problem. The final solution ended up being the following:
\A[^.]*\.\s\K[^.]*(?=\.)
Translation:
\A
declares start of string
[^.]*
says that you can have as many characters or whitespaces (as long as they are before the first period)
\.\s
finds the first period and whitespace
\K
then includes everything after that in the expression
[^.]*
includes every character or whitespace until the first period
(?=\.)
states to stop after the first period following the beginning (marked by \K)
Thanks again for the help!
Upvotes: 1
Reputation: 2258
This should work. It captures the second sentence as "Hopefully SO will help me out here".
\.\s*([\w\s]*)\.
Upvotes: 1