regex question

E

eldwindollente

Hi all,

I'm new to regex. I have a string of 32 characters and i need to
ignore the first 28 characters, change the next 2 (29, 30) and ignore
the last 2 (31,32.)

I have something like this so far s/AD01.{28}/xx/ but that's just
changing the 28 characters to XX.

Any help would be appreciated.

Thanks
 
J

Jürgen Exner

I'm new to regex. I have a string of 32 characters and i need to
ignore the first 28 characters, change the next 2 (29, 30) and ignore
the last 2 (31,32.)

Why do you want to use regular expressions for this task?
I have something like this so far s/AD01.{28}/xx/ but that's just
changing the 28 characters to XX.

Any help would be appreciated.

What about a simple

my $s = '12345678901234567890123456789012';
substr($s, 28, 2, 'XY');
print $s;

jue
 
P

Peter J. Holzer

I'm new to regex. I have a string of 32 characters and i need to
ignore the first 28 characters, change the next 2 (29, 30) and ignore
the last 2 (31,32.)
[...]
What about a simple

my $s = '12345678901234567890123456789012';
substr($s, 28, 2, 'XY');

Or:

substr($s, 28, 2) = 'XY';
print $s;

hp
 
M

Mirco Wahab

Hi all,

I'm new to regex. I have a string of 32 characters and i need to
ignore the first 28 characters, change the next 2 (29, 30) and ignore
the last 2 (31,32.)

I have something like this so far s/AD01.{28}/xx/ but that's just
changing the 28 characters to XX.

Jürgen already wrote the appropriate solution
and I'll add the regex here (in case of ...)

Lets start with a 32 char string of alternating slashes:

my $string = '\/\/' x 8; # 32 characters ('x' repeats 'wave')

To replace string[28->29], we could do a:

$string =~ s/ (?<=.{28}) .. /XX/x;

which means:

.. ==> replace 2 characters

(?<=.{28}) ==> if exactly 28 characters are in front of them



Another possibility would be to
set the next match position explicitly:

...
my $string = '\/\/' x 8; # 32 characters (repeats 'wave')

pos($string) = 28; # set match position
$string =~ s/\G../XX/x; # change 2 characters there
...

pos(x) sets the next regex trial position
and \G anchor forces the regex to appreciate it.


Regards

Mirco
 
U

Uri Guttman

PJH> On 2007-03-19 17:30 said:
I'm new to regex. I have a string of 32 characters and i need to
ignore the first 28 characters, change the next 2 (29, 30) and ignore
the last 2 (31,32.)
PJH> [...]
What about a simple

my $s = '12345678901234567890123456789012';
substr($s, 28, 2, 'XY');

PJH> Or:

PJH> substr($s, 28, 2) = 'XY';

4 arg substr is faster than lvalue substr and noticeably so the last
time i checked. it makes sense as 4 arg substr is one internal perl op
but lvalue substr needs to make a substr ref thing and then overwrite it
with the assignment. it has at least 2 op calls there vs 1 for 4 arg
substr. a deparse would probably show this too.

uri
 
J

Josef Moellers

Hi all,

I'm new to regex. I have a string of 32 characters and i need to
ignore the first 28 characters, change the next 2 (29, 30) and ignore
the last 2 (31,32.)

I have something like this so far s/AD01.{28}/xx/ but that's just
changing the 28 characters to XX.

s/(.{28})..(.*)/$1xx$2/

$s = 'abcdefghijklmnopqrstuvwxyz012345';
$s =~ s/(.{28})..(.*)/$1xx$2/;
print "$s\n";
abcdefghijklmnopqrstuvwxyz01xx45
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top