Conditional expressions and replacement

General questions about using TextPad

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

Post Reply
melvers
Posts: 26
Joined: Tue Aug 05, 2003 1:52 pm

Conditional expressions and replacement

Post by melvers »

I have tab-delimited text. Certain columns have YES or NO values. I am trying to replace the YES or NO values with 1 or 0. There are too many columns to do all the permutations of YES or NO in the find expression. I was looking at using the conditional expressions but I just can't understand the Textpad Help enough to figure our how it works.

Here is an small example of the original data.

0 0 0 1 NO NO YES NO 0 0 0 0 0

I want it to be

0 0 0 1 0 0 1 0 0 0 0 0 0

Can anyone tell me if this is even possible in a single (of very few) replacement operations. If it is can you give me or direct me to a fuller description of the use of conditional expressions in Textpad (preferably with examples).
lklawrie
Posts: 52
Joined: Fri Aug 22, 2003 3:19 pm
Location: Pagosa Springs, Colorado, USA
Contact:

Post by lklawrie »

maybe this is too simple but can't you just look for:

\tNO
and replace with
\t0

?
Linda
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

As Linda says, you can replace all occurrences of NO with 0 and all occurrences of YES with 1.

You can do both in a single replacement:
Find what: \b(?:(YES)|(NO))\b
Replace with: ?1(1):?2(0)

[X] Regular expression

Replace All
The regular expression \b(?:(YES)|(NO))\b matches

Code: Select all

\b         the start or end of a word
(?:        (start of non-capturing subexpression)
  (          (start of capturing subexpression number 1)
    YES        the text "YES"
  )          (end of capturing subexpression number 1)
|          or
  (          (start of capturing subexpression number 2)
    NO         the text "NO"
  )          (end of capturing subexpression number 2)
)          (end of non-capturing subexpression)
\b         the start or end of a word
The replacement expression ?1(1):?2(0) specifies that the matched text is replaced with

Code: Select all

?1         if subexpression number 1 matched then 
  (1)        the text "1"
:          else
?2         if subexpression number 2 matched then 
  (0)        the text "0"
Post Reply