Forum Moderators: coopster & phranque

Message Too Old, No Replies

Search and Replace problem

search and replace (regx)

         

bg01au

7:12 am on Sep 23, 2005 (gmt 0)

10+ Year Member



Eg. I have a dynamic string "gdakfdjasklf dkaf KeepMe blahblahblah"
The expected result is "KeepMe blahblahblah"

Basically I want to search the string and remove everything before "KeepMe blahblahblah"

Please help and thanks muchly.

Moby_Dim

8:48 am on Sep 23, 2005 (gmt 0)

10+ Year Member



my($result) = $source_string =~ m/^.+?(KeepMe.+)$/

KevinADC

6:22 pm on Sep 23, 2005 (gmt 0)

10+ Year Member



since it looks like you are not searching for a pattern, but a substring, it should be faster to use substr() and index().


my $str = 'gdakfdjasklf dkaf KeepMe blahblahblah';
my $newstr = substr($str,index($str,'KeepMe'));
print $newstr;

bg01au

12:14 am on Sep 27, 2005 (gmt 0)

10+ Year Member



Thank you very much for your help. Cheers.

bravetanveer

4:31 pm on Oct 4, 2005 (gmt 0)

10+ Year Member



To add to search and replace:

I am facing problem with replacing one of the metacharacter i.e. '{'.

I am searching for a pattern::

#ifdef ABC
{

abclkckl();

}

This is a typical pattern in .c file.
and want to replace it with::

#ifdef ABC

#ifdef XY
tutu();
#endif
{
abclkckl();
}
#ifdef XY
tutu1();
#endif

#endif

Here the original pattern is as it is, and we can replace "{" with #ifdef XY tutu(); #endif

I started my quest with::

$start = "#ifdef XY\ntutu\(\)\;\n#endif\n";

s/(#ifdef\s+ABC)\s+/$1\n$start/g;

*******************
With this i was able to get:

#ifdef ABC

#ifdef XY
tutu();
#endif

{
abclkckl();
}
#endif

*************************

I was not able to replace '{' meta character.

I tried using

s/(#ifdef\s+ABC)\s+\{/$1\n$start/g;

as \ can escape metacharacters, i.e. it makes them behave as literals, so i used this sequence.

Help me out

Thanks in advance.

bravetanveer

5:07 pm on Oct 4, 2005 (gmt 0)

10+ Year Member



Well, just 2 minutes of thinking gave me results.

my pattern code was

$/=""; # Make it take input in paragraph
And this was my problem, i was parsing the input file line by line.

If i take input in paragraph then the code below gave me perfect results

s/(#ifdef\s+ABC)\s+(\{)/$1\n$start$2/g;

Thanks anyways.

KevinADC

5:18 pm on Oct 4, 2005 (gmt 0)

10+ Year Member



next time you should sart a new thread. :)

There is nothing speacial about { and } in the regular expression you posted. Here is an example:


my $str = "{}text{}text";
$str =~ s/{/[/g;
$str =~ s/}/]/g;
print $str;

prints:

[]text[]text

<edit>

ahh, you solved your problem, very good!

</edit>

bravetanveer

2:40 pm on Oct 8, 2005 (gmt 0)

10+ Year Member



One more problem i am facing now

For the search and replace pattern i mentioned above, i have one doubt.

I have a pattern::

#ifdef do_dod
{
try((unsigned int)(&(Array1[0])), (unsigned int)(&(Array2[0])), (unsigned int)(&(Array3[0])));
}
#else

for(j=0;j<LEN;j++)
{
Array3[j] = Array1[j] + Array2[j];
Array1[j] = Array2[j] + Array3[j];
}
#endif

I was able to replace it with ::::

$Call = "try((.*";

$Hello = "my hello";
$Hello1 = "my hello1";

$_ =~ s/(#ifdef\s+do_dod)\s+(\{)\s+($Call)\s+(\})\s+(#else)(\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*))/$1\n$Hello\n$2\n\n$3\n\n$Hello1\n$4\n\n$5\n\n$Hello\n\n$6\n\n$Hello1\n\n/g;

#ifdef do_dod
my hello
{
try((unsigned int)(&(Array1[0])), (unsigned int)(&(Array2[0])), (unsigned int)(&(Array3[0])));
}
my hello1

#else
my hello
for(j=0;j<LEN;j++)
{
Array3[j] = Array1[j] + Array2[j];
Array1[j] = Array2[j] + Array3[j];
}
my hello1
#endif

************************

But the problem here is:::

\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*))

Can we have anything else for this recurring pattern.
One more problem is , we don't know how many times it will comes.

This pattern is for

*********************
sdfkljsdljsdfldj
sdfl;jsljks
lsjdflsjkdl
***********************

i.e. to detect each line and we don't know how many lines will be present.

Thanks in advance


With this i am able to get desired result of::

KevinADC

9:26 pm on Oct 8, 2005 (gmt 0)

10+ Year Member



You might want to look at beginning and ending patterns instead of long ambiguous pattern searches. Something like:

if (/startpattern/ .. /endpattern/) {
do something
}