tags:

views:

142

answers:

5
A: 

Not c#, but you should get the idea

for sentence in split_text_with_regex(text):
    index_word1 = sentence.find(word1)
    index_word2 = sentence.find(word2)
    # do your thing
Otto Allmendinger
Is it possible to costruct one regex with no for's?
StuffHappens
A: 

There is a very good set of options available here http://www.regular-expressions.info/near.html

Subhash Dike
the link is helpfull but it doesn't solve my problem as far as \W might match a period.
StuffHappens
A: 

Also you can construct the regular expression in Visual Studio itself . Refer to this link http://msdn.microsoft.com/en-us/library/2k3te2cs(VS.80).aspx first paragraph

Subhash Dike
A: 

So I think it's something like this (untested):

(([\w\s]*\s)?Word1\s([\w\s]*)?\sWord2(\s[\w\s]*)?\.)(?=(\s+[A-Z]|\s*$))

Edit: Thinking about it, that won't match punctuation (commas, apostrophes). Perhaps each [\w\s] should be [^\.] or a list of possible characters.

pdr
+1  A: 

You could use this:

(\b\w+\b)(?:[^.]|\.\s)*(\b\w+\b)

This basically says, match and capture a word, then anything that is not a period, or a period followed b a space, any number of times, and finally match and capture another word.

EDIT: For given words in either order, use:

(\bWord1\b)(?:[^.]|\.\s)*(\bWord2\b)|(\bWord2\b)(?:[^.]|\.\s)*(\bWord1\b)
Max Shawabkeh