Regular expression problem

H

heather

I have a problem that I believe can be solved with the right regular
expression, but I'm not having luck with it.

Given a data stream example of:

123 AG 45675 0987 AA 618 123A721B123A041B193A711B173A091B

I need to change it to be:

123 ABC 4567 0987 AA 618 123A 721B 123A 041B 193A 711B 173A 091B

Basically, separate the last value with a space after each letter.

The data will also be in the format as the example, where the letters I
need to separate will always be A or B. There could be an existance of
an A or B elsewhere in the line, and those aren't to be separated.

Tanks in advance!
Heather
 
X

xhoster

I have a problem that I believe can be solved with the right regular
expression, but I'm not having luck with it.

Given a data stream example of:

123 AG 45675 0987 AA 618 123A721B123A041B193A711B173A091B

I need to change it to be:

123 ABC 4567 0987 AA 618 123A 721B 123A 041B 193A 711B 173A 091B

Why did the G of AG and 5 of 45675 get changed?
Basically, separate the last value with a space after each letter.

The data will also be in the format as the example, where the letters I
need to separate will always be A or B. There could be an existance of
an A or B elsewhere in the line, and those aren't to be separated.

$x =~ s/([AB])(?=[^ ]*$)/$1 /g;

123 AG 45675 0987 AA 618 123A 721B 123A 041B 193A 711B 173A 091B


Xho
 
U

usenet

I have a problem that I believe can be solved with the right regular expression

Maybe one like this:

#!/usr/bin/perl

use warnings; use strict;
my $data =
'123 AG 45675 0987 AA 618 123A721B123A041B193A711B173A091B';

$data =~ s/(\d+[AB])(?=.)/$1 /g;

print "$data";

__END__

The expression says
match one or more digits
followed by either A or B
(but only if there is something following A or B - ie, not
end-of-line)
and replace the digits+letter with the digits+letter and a space.
 
M

Matt Garrish

I have a problem that I believe can be solved with the right regular
expression, but I'm not having luck with it.

When someone posts no code, the assumption is that person also hasn't tried.
Even if your code doesn't work, people will appreciate seeing that you've
attempted to solve it on your own.
Given a data stream example of:

123 AG 45675 0987 AA 618 123A721B123A041B193A711B173A091B

I need to change it to be:

123 ABC 4567 0987 AA 618 123A 721B 123A 041B 193A 711B 173A 091B

Basically, separate the last value with a space after each letter.

Those strings aren't the same. What is the rule for transforming 'AG' to
'ABC' or 45675 to 4567?
The data will also be in the format as the example, where the letters I
need to separate will always be A or B. There could be an existance of
an A or B elsewhere in the line, and those aren't to be separated.

I'd personally just do a couple of splits and a join:

my @start = split(/ /, '123 AG 45675 0987 AA 618
123A721B123A041B193A711B173A091B');
my @end = split(/([0-9]+[AB])/, $start[$#start]);

print join(' ', @start[0..$#start-1], @end);


Matt
 
H

heather

Good catch!

The change was my fault, a cut/paste error. Too long of a day!

Should be:

123 AG 45675 0987 AA 618 123A721B123A041B193A711B173A091B
becomes
123 AG 45675 0987 AA 618 123A 721B 123A 041B 193A 711B 173A 091B


Thanks
Heather


Matt said:
I have a problem that I believe can be solved with the right regular
expression, but I'm not having luck with it.

When someone posts no code, the assumption is that person also hasn't tried.
Even if your code doesn't work, people will appreciate seeing that you've
attempted to solve it on your own.
Given a data stream example of:

123 AG 45675 0987 AA 618 123A721B123A041B193A711B173A091B

I need to change it to be:

123 ABC 4567 0987 AA 618 123A 721B 123A 041B 193A 711B 173A 091B

Basically, separate the last value with a space after each letter.

Those strings aren't the same. What is the rule for transforming 'AG' to
'ABC' or 45675 to 4567?
The data will also be in the format as the example, where the letters I
need to separate will always be A or B. There could be an existance of
an A or B elsewhere in the line, and those aren't to be separated.

I'd personally just do a couple of splits and a join:

my @start = split(/ /, '123 AG 45675 0987 AA 618
123A721B123A041B193A711B173A091B');
my @end = split(/([0-9]+[AB])/, $start[$#start]);

print join(' ', @start[0..$#start-1], @end);


Matt
 
R

robic0

Good catch!

The change was my fault, a cut/paste error. Too long of a day!

Should be:

123 AG 45675 0987 AA 618 123A721B123A041B193A711B173A091B
becomes
123 AG 45675 0987 AA 618 123A 721B 123A 041B 193A 711B 173A 091B


Thanks
Heather

No problem...

use strict;
use warnings;

my $dataline = '123 AG 45675 0987 AA 618 123A721B123A041B193A711B173A091B';

while ($dataline =~ s/([ab]+?)([0-9]+?[ab0-9]*?)$/$1 $2/ig) {}

print $dataline,"\n";

__END__

123 AG 45675 0987 AA 618 123A 721B 123A 041B 193A 711B 173A 091B
 
R

robic0

Good catch!

The change was my fault, a cut/paste error. Too long of a day!

Should be:

123 AG 45675 0987 AA 618 123A721B123A041B193A711B173A091B
becomes
123 AG 45675 0987 AA 618 123A 721B 123A 041B 193A 711B 173A 091B


Thanks
Heather

No problem...

use strict;
use warnings;

my $dataline = '123 AG 45675 0987 AA 618 123A721B123A041B193A711B173A091B';

while ($dataline =~ s/([ab]+?)([0-9]+?[ab0-9]*?)$/$1 $2/ig) {}
while ($dataline =~ s/([ab]+?)([0-9]+?.*?)$/$1 $2/ig) {}

In case there might be imbedded 'G's an stuff
Any more complex a new string would have to be build from pieces
within the while body (and without s// modifier).
 
A

Anno Siegel

I have a problem that I believe can be solved with the right regular
expression, but I'm not having luck with it.

Given a data stream example of:

123 AG 45675 0987 AA 618 123A721B123A041B193A711B173A091B

I need to change it to be:

123 ABC 4567 0987 AA 618 123A 721B 123A 041B 193A 711B 173A 091B

Basically, separate the last value with a space after each letter.

my $str = "123 AG 45675 0987 AA 618 123A721B123A041B193A711B173A091B";
substr( $str, rindex( $str, ' ')) =~ s/([AB])/$1 /g;
print "$str\n";

Anno
 

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

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top