0

I want to compare string from one file to another. but another file may contains some element and that element can occur anywhere and it can occur many times also.

Note : these tags needs to be retain in final output.

For e.g.: I want to compare word ‘scripting’.. tag indicates the word to be matched from str2.

$str1 = “perl is an <match>scripting</match> language”;
$str2 = “perl is an s<?..?>cr<?..?>ipti<?..?>ng langu<?..?>age”;

Output required : perl is an <match>s<?..?>cr<?..?>ipti<?..?>ng</match> langu<?..?>age

I am adding pattern after each character:

$str1 =~ {(.)}
{
‘$&(?:(?:<?...?>|\n)+)?’
}esgi;

These works for few case but for few its goes on running. Please suggest.

6
  • Maybe I am missing someting - cant you just remove <?..?> from str2 and if it compares return the original str2 Commented Mar 3, 2014 at 9:17
  • Can you give a full example of the input that causes the text program to look. Also it would help to quote the \Q<?..?>\E - the embedded ? and . maybe making the job harder than it might be Commented Mar 3, 2014 at 9:19
  • @justintime no <?..?> cannot be removed because scripting word can be present many times in string2... i am playing with two different files and the word may be repeated. Commented Mar 3, 2014 at 9:21
  • 2
    Where do these kind of requirements come from? Commented Mar 3, 2014 at 20:57
  • @justintime just i want to add pattern around each character and that pattern is optional..it may or may not present in str2 Commented Mar 4, 2014 at 5:53

1 Answer 1

1

(?:(?:<?...?>|\n)+)? is the same as (?:<?...?>|\n)*
Also you dont want to add the pattern after each character; just in between the characters of the matched part of $str1. So no pattern before the first character and no pattern after the last. Otherwise the replace statement will around those tags, and you want them to around the front and back of the words. My guess is that if you are runnging that first replace command over all of $str1 you may end up with quite a large string.
Also see my answer for related question here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.