Regex Precedence

K

Kevin Nechodom

I am trying to format some text in the midst of a s/// substitution. I have a field that can be:
(blank)
char
char-num, char - num, char#num
num
num-num, num - num, num#num

and I want to reformat it to:
(char or num)-num

So I'm trying to match it with:
s/(\w*)(?: |-|\#)*(\w*)/$1-$2/

But when I come to the case of '632', it returns '-632' instead of '632-'. How do I change this to return '632-'? I dont' understand regex precedence here.

Thanks,
 
A

A. Sinan Unur

<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2900.2769" name=GENERATOR></HEAD>

Please don't.

Sinan
 
J

John W. Krahn

Kevin said:
I am trying to format some text in the midst of a s/// substitution. I have a
field that can be:
(blank)
char
char-num, char - num, char#num
num
num-num, num - num, num#num

and I want to reformat it to:
(char or num)-num

So I'm trying to match it with:
s/(\w*)(?: |-|\#)*(\w*)/$1-$2/

But when I come to the case of '632', it returns '-632' instead of '632-'. How
do I change this to return '632-'? I dont' understand regex precedence here.

This may be what you want:

s/(\w+)(?: *[#-] *)?(\d*)/$1-$2/g;



John
 
M

Matt Garrish

Kevin Nechodom said:
I am trying to format some text in the midst of a s/// substitution. I
have a field that can be:
(blank)
char
char-num, char - num, char#num
num
num-num, num - num, num#num
and I want to reformat it to:
(char or num)-num
So I'm trying to match it with:
s/(\w*)(?: |-|\#)*(\w*)/$1-$2/
But when I come to the case of '632', it returns '-632' instead of '632-'.
How do I change
this to return '632-'? I dont' understand regex precedence here.

Please do not post html messages to usenet, and vcf files are equally
unwanted.

My guess is that it has to do with making the content inside your first
capture optional (you might want to consider reading Mastering Regular
Expressions). It doesn't have to match, which means that your look-ahead
assertion can, and if it is followed by any \w characters those characters
will come out after the dash in your replacement. For example:

$num = ' -#632';
$num =~ s/(\w*)(?: |-|\#)*(\w*)/$1-$2/;
print $num;

Without seeing any of the actual data you're working with, however, I'm not
going to speculate at what will or will not fix your problem.

Matt
 
K

Kevin Nechodom

Matt Garrish<[email protected]> 10/31/2005
6:43 PM >>>

in message
precedence here.
Please do not post html messages to usenet, and vcf files are equally
unwanted.
I'll do my best, but I won't see it (HTML) Please let me
know if the problem persists.
Without seeing any of the actual data you're working with, however, I'm not
going to speculate at what will or will not fix your
problem.
My bad! Text was being interpreted, so rhat '632-' was
veing displayed -632.

Thanks,

--
Kevin Nechodom
University of Utah Hospital and Clinics
Kevin dit Nechodom ack hsc dit utah dit edu
"Call me paranoid, but I think you are reading what I'm
writing!"
 

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,770
Messages
2,569,586
Members
45,083
Latest member
SylviaHarr

Latest Threads

Top