0

I am stuck at one more RegEx.

Sample input :

project description

I want to write RegEx. that if word "description" is found and its preceded by word "project" then prepend "project description" with "<project>".

expected output :

<project>project description

I wrote following Regex, n replacement string but its not working : regex : ((?<=project) description) and replacement string : <project_description>$0

With current regex and replacement string im getting following output :

project<project> description

Can anyone suggest something?


**

EDITED QUESTION

** Ok, I think I am not clear enough about telling people what I want exactly.

To make the problem generic and clearer, I will put it in another way.

If "y" comes after "x", then prepend <tag> to xy.

Is it possible to do this using regular expressions?

Shekhar
  • 11,438
  • 36
  • 130
  • 186

3 Answers3

3

I don't understand why you're using a lookbehind for this - you can just do:

/project description/  ->  <project>$0

In PHP this would be

echo preg_replace('/project description/', '<project>$0', $str);
Greg
  • 316,276
  • 54
  • 369
  • 333
  • Hi Greg, the text that i have given in question is just a sample text. Actually text might be any of the following forms: 1) Details of project or 2) project(s) summary or 3) project or 4) projects undertaken. For all the above i want to prepend to those words. – Shekhar Oct 26 '09 at 13:04
2

Extending Greg's regex to match the newly added cases:

/Details\sof\sproject|project(\sdescription|s\sundertaken|\(s\)\ssummary)/

//replace all the matches with

<project>$0

You can later add new cases to this regex using |.


EDIT: To answer If "y" comes after "x", then prepend <tag> to xy.

Replace /x\s\y/ with <tag>$0

This will prepent <tag> to all occurrences of x-space-y

Amarghosh
  • 58,710
  • 11
  • 92
  • 121
1

EDITED ANSWER:

This doesn't seem appropriate for lookbehind. Why don't you just replace the whole thing, like this:

s/(project|other|word) description/<project>$1 description/

This will prepend the word <project> to any of project, other, or word if it is followed by description.

Avi
  • 19,934
  • 4
  • 57
  • 70
  • Actually, the text that i have given in question is just a sample text. Actually text might be any of the following forms: 1) Details of project or 2) project(s) summary or 3) project or 4) projects undertaken. For all the above i want to prepend to those words. – Shekhar Oct 26 '09 at 13:09
  • 1
    Edit the question and add these conditions. – Amarghosh Oct 26 '09 at 13:35
  • @Amarghosh, It is not possible to provide all the possibilites of text that will occure in file because even I dont know what will occur in later stages. To make the problem generic and clearer, I will put it in another way If "y" comes after "x", then prepend to xy. Is it possible to do this using regular expressions? – Shekhar Oct 26 '09 at 13:49