Regex Issue: Removing all or part of a pattern from the start of a string

H

Hal Vaughan

I know this is simpler than I'm making it out to be, but maybe I've just not
had enough sleep lately. I have data coming to me that has already been
pulled off a VT102 display. Normally the display reads:

MAILADR 1234 Some Street, Rd.

Previously the data that was coming in used to be clean. There was no extra
data so it was just a matter of making sure the spaces at the start and end
were cleared. Now sometimes I get it with "R" at the start of the data,
like this:

R 1234 Some Street, Rd.

And sometimes it's ADR or DR at the start. I keep thinking one regex should
work to pull out MAILADR, AILADR, ILADR, and so on from the start, but I
keep missing it.

I can just use different regexes, but it's bugging me now. Isn't there a
quick way to do this? I know if I try:

$data =~ s/^M*A*I*L*A*D*R* *//

it will usually do the trick, but it could get other expressions as well so
what I don't get is making sure it pulls only R or only DR or ADR and so
on.

This is fairly simple, right? Or am I confused because of lack of sleep?

Thanks for a pointer in some direction or help or anything else!

Hal
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Stefan Ram schreef:
|> MAILADR 1234 Some Street, Rd.
|> R 1234 Some Street, Rd.
|> $data =~ s/^M*A*I*L*A*D*R* *//
|
| $data =~ s/^(?:(?:(?:(?:(?:(?:M)?A)?I)?L)?A)?D)?R //;

Why not simply
"^M?A?I?L?A?D?R "
?

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFIUOaue+7xMGD3itQRAmPaAJ9ugOf2/z12LTJqmsDfYc0NS1esgACeOIKF
BPeStkvY2W0GWvRrlwAJnZo=
=mky8
-----END PGP SIGNATURE-----
 
J

Jussi Piitulainen

Hendrik said:
Stefan Ram schreef:
| Hal Vaughan writes:
|> MAILADR 1234 Some Street, Rd.
|> R 1234 Some Street, Rd.
|> $data =~ s/^M*A*I*L*A*D*R* *//
|
| $data =~ s/^(?:(?:(?:(?:(?:(?:M)?A)?I)?L)?A)?D)?R //;

Why not simply
"^M?A?I?L?A?D?R "
?

Because the examples sort of looked like only suffixes of MAILADR
should be removed, and MILD, for example, isn't one.

(One could grab a prefix of the line up to the first space and check
if it's a suffix of MAILADR, but then this wouldn't be a Regex Issue.)
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top