s/(The N)e(urdsburg Affair)/$1$2/ looks dumb

J

jidanni

Sure I can do
s/(The N)e(urdsburg Affair)/$1$2/;
but something tells me that there ought to be a smarter looking way to
get the same functionality. Something like
s/The N(e)urdsburg Affair/... but what?
 
P

Peter Wyzl

Sure I can do
s/(The N)e(urdsburg Affair)/$1$2/;
but something tells me that there ought to be a smarter looking way to
get the same functionality. Something like
s/The N(e)urdsburg Affair/... but what?

Why not just s/Neurdsburg/Nurdsburg/; ?

P
 
S

sln

Sure I can do
s/(The N)e(urdsburg Affair)/$1$2/;
but something tells me that there ought to be a smarter looking way to
get the same functionality. Something like
s/The N(e)urdsburg Affair/... but what?

This is not a suitable example of the use of regular expression
substitution. Regular expressions are not used for in-line
spelling corrections, especially for book/movie titles.
Unless you want to do a million different regular expressions
from pos() = 0, substitutions are something that should be used
possibly multiple times.

The regular expression 'substitution' idiom is valid as always.
Its up to you how its used.

The rule is only use as much as necessary to uniquely describe what you are
looking for in the target string. This possibly reduces the search
overhead. But the entire match will be affected with whatever you
direct on the substitution.

-sln
 
N

Nathan Keel

Sure I can do
s/(The N)e(urdsburg Affair)/$1$2/;
but something tells me that there ought to be a smarter looking way to
get the same functionality. Something like
s/The N(e)urdsburg Affair/... but what?

Why would you capture (e) and not use it?

Just

s/The Neurdsburg Affair/The Nurdsburg Affair/

Or whatever makes the best sense, if that's how you really want to do
it.
 
T

Ted Zlatanov

On Thu, 14 May 2009 11:00:28 +0800 (e-mail address removed) wrote:

j> Sure I can do
j> s/(The N)e(urdsburg Affair)/$1$2/;
j> but something tells me that there ought to be a smarter looking way to
j> get the same functionality. Something like
j> s/The N(e)urdsburg Affair/... but what?

I've learned, with experience, to beware "smart" ways of solving simple
problems. They usually become unmanageable messes, all for the sake of
stroking the programmer's ego. I've been guilty of this too.

It's almost always (99.9% of the time) better to just do what others
would expect you to do, even if it's not emphatically clever. As the
famous quote goes, code as if the person who will maintain your code is
a homicidal maniac who knows where you live.

Ted
 
S

sln

Why would you capture (e) and not use it?

Just

s/The Neurdsburg Affair/The Nurdsburg Affair/

Or whatever makes the best sense, if that's how you really want to do
it.

Why not capture something when you already know what it is?
Why not a single letter in the sequence.
Maybe $-[1] is something you need to know. Maybe you want to
rip out or assign substr style.

Been at this very long?

-sln
 
S

sln

On Thu, 14 May 2009 11:00:28 +0800 (e-mail address removed) wrote:

j> Sure I can do
j> s/(The N)e(urdsburg Affair)/$1$2/;
j> but something tells me that there ought to be a smarter looking way to
j> get the same functionality. Something like
j> s/The N(e)urdsburg Affair/... but what?

I've learned, with experience, to beware "smart" ways of solving simple
problems. They usually become unmanageable messes, all for the sake of
stroking the programmer's ego. I've been guilty of this too.

It's almost always (99.9% of the time) better to just do what others
would expect you to do, even if it's not emphatically clever. As the
famous quote goes, code as if the person who will maintain your code is
a homicidal maniac who knows where you live.

Ted

I like this.

-sln
 
N

Nathan Keel

Why would you capture (e) and not use it?

Just

s/The Neurdsburg Affair/The Nurdsburg Affair/

Or whatever makes the best sense, if that's how you really want to do
it.

Why not capture something when you already know what it is?
Why not a single letter in the sequence.
Maybe $-[1] is something you need to know. Maybe you want to
rip out or assign substr style.

Been at this very long?

-sln

The OP wants to replace a mistyped (specific) phrase with the correct
one. Why the hell would anyone wants to capture $1 and $2, just to
print it out, when they can just replace as I suggested above? WHY
would someone WANT to "capture" something when they already know what
it is, if there's no relation to the task they want to achieve?
You're not making, read what the OP posted.
 

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
474,214
Messages
2,571,112
Members
47,704
Latest member
DavidSuita

Latest Threads

Top