How do i replace tagged expression on OR conditional search

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
thamark
Posts: 3
Joined: Thu Jan 15, 2009 7:55 pm

How do i replace tagged expression on OR conditional search

Post by thamark »

Hi,

I am trying to find and replace following comma(,) and rest of the line on following three situation of select query and i got struck at the replacement speicification.

1) Line ended with comma(,)
Example: a.column3,
2) Line ended with spaces( ) after comma(,)
Example: a.column,\space\space\space
3) Line ended with comments(--some text spaces) after comma
Example: a.column2, -- comment,

Now i want above three scenarios to be replaced with text just before that comma(,) character.

I could able to find all of them with the following find specification.

Find What: ^\(.*\),[ ]*$\|^\(.*\),[[:blank:]]*--*.*$

Replace with: \1\2

This didn't work well when it finds the string by first expression itself.

I could do this in two search, but i would prefer to do it in single search itself.Please advise me on this....

Thanks
Thamark
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

I recommend Posix regular expression syntax, as it reduces the number of backslashes and increases readability:
Configure | Preferences | Editor

[X] Use POSIX regular expression syntax
Then:
Find what: ,[[:blank:]]*(--.*)?$
Replace with: [nothing]

[X] Regular expression

Replace All
thamark
Posts: 3
Joined: Thu Jan 15, 2009 7:55 pm

It didn't work

Post by thamark »

Ben,

I tried following things and it didn't work.
Configure | Preferences | Editor

[X] Use POSIX regular expression syntax
Tried to do your find and replace for following sample data and it didn't work.
a.column1,
a.column2,
'',
1, -- comment,
'', -- comment,
'', -- comment,
'', -- comment
It didn't find those string at all. My textpad version is Textpad 4.7.3 : 32-bit version.

Result should be like
a.column1
a.column2
''
1
''
''
''
Thanks for your reply and i will be looking forward to see update in this regard...
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

I can assure you that it does work.

Make sure you remove any spurious trailing space that the web page may stick on the end of the search expression.
thamark
Posts: 3
Joined: Thu Jan 15, 2009 7:55 pm

Hmm i got it wrong somewhere

Post by thamark »

Ben,

I tried it again and it works perfectly. I think as you mentioned something added when I was copying from web to tool.

Thanks a lot and I never thought of this solution, since I was struck that I should be handling them as individual cases....

By the way do we have any answer for how to replace OR condition cases just curios
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

In the general case you can't do this.

Suppose you have an alternation (a regex with a | in it):
E11 (E12) E13 | E21 (E22) E23
so that, in a replacement, \1 is whatever E12 matches and \2 is whatever E22 matches.

Then if the first alternative ( E11 (E12) E13 ) matches, so that \1 is whatever E12 matches, then \2 simply doesn't exist, and if there is a \2 in the replacement it is included literally.

And if the second alternative ( E21 (E22) E23 ) matches, so that \2 is whatever E22 matches, then \1 simply doesn't exist, and if there is a \1 in the replacement it is included literally.
Evil Bob
Posts: 3
Joined: Sat Feb 14, 2009 3:10 am

Post by Evil Bob »

You could always capture everything before the first comma.

POSIX Example:

Find:::^([^,]+),.+$
Replace:::\1

Or if you needed everything before a second comma...

Find:::^([^,]+,[^,]+),.+$
Replace:::\1
Post Reply