views:

56

answers:

2

Hi,

I'm using the following regular expression to find the exact occurrences in infinitives. Flag is global.

(?!to )(?<!\w) (' + word_to_search + ') (?!\w)

To give example of what I'm trying to achieve

  • looking for out should not bring : to outlaw
  • looking for out could bring : to be out of line
  • looking for to should not bring : to etc. just because it matches the first to

I've already done these steps, however, to cross out/off should be in the result list too. Is there any way to create an exception without compromising what I have achieved?

Thank you.

A: 

I know OR operator but the question was rather how to organize the structure so it can look ahead and behind. I'm going to explain what I have done so far

var strPattern:String = '(?!to )(?<!\w) (' + word_to_search + ') (?!\w)|';
strPattern+='(?!to )(?<!\w) (' + word_to_search + '\/)|';
strPattern+='(?!to )(\/' + word_to_search + ')';
var pattern:RegExp = new RegExp(strPattern, "g");

First line is the same line in my question, it searches structures like to bail out for cases where you type out. Second line is for matching structures like to cross out/off. But we need something else to match to cross out/off if the word is off. So, the third line add that extra condition.

kubarium
+1  A: 

I'm still not sure I understand the question. You want to match something that looks like an infinitive verb phrase and contains the whole word word_to_search? Try this:

"\\bto\\s(?:\\w+[\\s/])*" + word_to_search + "\\b"

Remember, when you create a regex in the form of a string literal, you have to escape the backslashes. If you tried to use "\b" to specify a word boundary, it would have been interpreted as a backspace.

Alan Moore
First of all, I'm using http://gskinner.com/RegExr/ to test. I tried your regex but it didn't bring any result. My example set is this :to cross out/offto outlineto get out ofto go outto be thoroughSo if I type th, I should not get anything. I type out, I should receive the first and the third one. I should type of or off and I should still get results so / should be treated like empty characters before the word I'm looking for.Sorry, I thought the editing would be better here but the bulleted list structure got lost in this comment section.
kubarium
You can always edit your question; formatting works great there.
Alan Moore
This one worked actually strPattern = '(?!to )\\b'+word_to_search+'\\b'; I can articulate more if anyone would like.
kubarium