1
votes

Is this possible in reg-ex?

subject='fox';
adjective='quick brown';
verb='jumps';
text='The <<adjective>> <<subject>> <<verb>> over the lazy dog.'

reg-ex would return:

'The quick brown fox jumps over the lazy dog.'

3
Isn't this just string concatenation?Josh M.
No. the text could be from a text file.cris

3 Answers

0
votes

You don't even need regular expressions to do that. I assume PHP has a string.Replace function of some sort, right? So wouldn't you just be doing (in C#, sorry):

text = "The <<adjective>> <<subject>> <<verb>> over the lazy dog.";
text = text.Replace("<<adjective>>", adjective);
text = text.Replace("<<subject>>", subject);
text = text.Replace("<<verb>>", verb);
1
votes

Use regex only when you need to. In this case, you don't:

var subject='fox';
var adjective='quick brown';
var verb='jumps';
var text='The <<adjective>> <<subject>> <<verb>> over the lazy dog.'

text = text.replace('<<adjective>>', adjective)
    .replace('<<subject>>', subject)
    .replace('<<verb>>', verb);

console.log(text);

Fiddle here

There's no need to use regex for a simple string replace (regex is quite a bit more expensive than native string functions).

0
votes

look into look into regex replace

You can probably find what you are looking for Here