Page 1 of 1

replace question

Posted: Thu Aug 09, 2001 11:59 pm
by Steve
hi,

If i have a txt document that looks like :-

-|(XP8750)|-random string
-|(XP8751)|-random string
-|(XP8766)|-random string
-|(XP8767)|-random string
-|(XP8771)|-random string
how can i remove every -|(XP8750)|- so i'm just left with random strings?
TIA

Re: replace question

Posted: Fri Aug 10, 2001 4:27 pm
by Roy Beatty
Regex Q's like this serve as crossword puzzles for me ...

Find:::: ^-|(XP[0-9]\{4\})|-
Replace: (leave field empty)

Across 36,

Roy

Re: replace question

Posted: Fri Aug 10, 2001 11:22 pm
by Ron. Chambers
Roy,
Where did you learn the methodology you used above?

I like crosswords too but must be speaking the wrong language with replace.

What is ^? And [0-9] ? \(4\) ?


Thanks,
RON C

Re: replace question

Posted: Sat Aug 11, 2001 6:19 pm
by Steve
i tried,
Find:::: ^-|(XP[0-9]\{4\})|-
Replace: (leave field empty)

and got a
cannont find literal string '^-|(XP[0-9]\{4\})|-\n' error

now i'm confused

Re: replace question

Posted: Mon Aug 13, 2001 11:34 am
by Jens Hollmann
I like regular expressions too!

In the "Find and Replace" dialog turn on the "regular expression" checkbox. Then it should word because TextPad does not search the literal string "^-|(XP[0-9]\{4\})|-" which is indeed not in the textfile but the regular expression that means this:

^ matches the beginning of a line
-|(XP matches these characters (no special meaning)
[0-9] matches one digit (from 0 to 9)
\{4\} matches the last expression (the one digit!) exactly 4 times
)|- matches these characters

More of this you can find in the Online-Help. This works only when you haven't selected POSIX-regular expressions in the settings. But this is the default anyway.

Re: replace question

Posted: Mon Aug 13, 2001 12:23 pm
by Roy Beatty
Steve: It is not a literal string. In the Find / Replace / Find-in-files dialogs there is a Regular Expression checkbox. Put a checkmark in it and try it again.

Ron:

"^" at the beginning of a string anchors the expression to the beginning of a text line, that is, it looks for the pattern following the ^ to start at the top of the file or just after a newline character.

Re: \{4\}, (curly braces) note that you asked me about \(4\) (parentheses), which is (are?) very, very different. Here is the explanation for "\{count\}" from TextPad's help topic, "How to Use Regular Expressions" : Matches the specified number of the preceding characters or expressions. Example: ho\{2\}p matches hoop, but not hop.

Regarding "[...]", I quote again from the same help topic: Any one of the characters in the brackets, or any of a range of characters separated by a hyphen (-), or a character class operator (see below). Examples: h[aeiou][a-z] matches hat, hip, hit, hop, and hut; [A-Za-z] matches any single letter; x[0-9] matches x0, x1, …, x9.

Since I wanted to match any digit, I used [0-9] withhout the x prefix (indicating a match on hex values). I could have used [0-9][0-9][0-9][0-9] or [[:digit:]]\{4\} but [0-9]\{4\} was the shortest alternative.

"Where did you learn the methodology you used above?"<br>
In 1996 or so, about *two years* after I got my TextPad license, I recall reading one person helping another with a find/replace operation similar to something I needed. I do not recall whether that exchange took place on Usenet, a Perl site, or an earlier manifestation of this forum (compuserve?). I took regex seriously from that point on. I can safely say that every hour I studied regex has saved me at least ten.

I recommend three sources to help you learn about regular expressions: <br>
1. The TextPad Help file (search on "regular"). <br>
2. A Regex FAQ maintained by that TextPad illuminary and all-round great fellow, Jeff Epstein: <br>
http://jeffyjeffy.com/textpad/documenta ... aq.html<br>
3. When you *really* get hooked, get hold of the O'Reilly book, Mastering_Regular_Expressions by Jeffry Friedl.

The only down-side to my addiction to regex has been to ignore some unrelated and very useful TextPad functionality. For example, a few months ago someone asked if TextPad could insert a column of line numbers with leading zeroes. I immediately turned to discovering a regex solution. Someone else pointed out TextPad's Edit > Fill Block function which is specifically designed for that purpose.

Well, enough of this unabashed geekiness.

Good luck,
Roy

Re: replace question

Posted: Mon Aug 13, 2001 12:26 pm
by Roy Beatty
Ah, someone else stepped in during my morning interruptions. Nice to see yet another "regular" guy!

Roy

Re: replace question

Posted: Mon Aug 13, 2001 5:18 pm
by steve
thanks for the replies ppl,
ok.
when i paste ' ^-|(XP[0-9]\{4\})|-\n ' into the find field, ' ^-|(XP[0-9]\{4\})|-\n ' comes up instead (\n). Is this correct?. If so when i replace i get the following error message.
'cannot find regular expression ^-|(XP[0-9]\{4\})|-\n '
If i paste it without the \n it removes all ' - ' from my listso i'm left with
|(XP8750)|random string...what can i be doing wrong?
Thanks again,
Steve

Re: replace question

Posted: Tue Aug 14, 2001 7:04 pm
by Roy Beatty
Remove all trailing blanks from the Find expression. In general, if a Find expression does not select as you expected, try removing terms from the right-hand side until the shortened pattern starts matching as you expect it to, then experiment with the later terms to understand what caused the original mismatch.