EmEditor (text editor) Forum Index
   Regular Expressions
     Example please for (?n:true_expression:false_expression)
Register To Post

Threaded | Newest First Previous Topic | Next Topic | Bottom
Poster Thread
Stefan
Posted on: 10/13/2011 10:51 pm
Home away from home
Joined: 7/14/2008
From: Germany, EU
Posts: 257
Example please for (?n:true_expression:false_expression)
How can i utilize this feature please?

(?n:true_expression:false_expression)


Found at:
http://www.emeditor.com/modules/feature1/rewrite/tc_35.html
EmEditor Professional 9 New Features
The replace format "(?n:true_expression:false_expression)"
was added to regular expression replace formats.
JohnQSmith
Posted on: 10/17/2011 12:10 pm
Not too shy to talk
Joined: 7/15/2011
From:
Posts: 30
Re: Example please for (?n:true_expression:false_expression)
I played with it a while and finally figured it out. Here's an example.

Input document
TheGreenAile TheGreenBile TheGreenCile TheGreenDile
TheGreenEile TheGreenFile TheGreenGile TheGreenHile
TheGreenIile TheGreenJile TheGreenKile TheGreenLile
TheGreenNile TheGreenOile TheGreenPile TheGreenQile
TheGreenRile TheGreenSile TheGreenUile TheGreenVile
TheGreenWile TheGreenXile TheGreenYile TheGreenZile

Search string
(?:([BFNPR])|([^BFNPR]))(ile)

Replace string
(?1:M:T)\3

Replace all and output is
TheGreenTile TheGreenMile TheGreenTile TheGreenTile
TheGreenTile TheGreenMile TheGreenTile TheGreenTile
TheGreenTile TheGreenTile TheGreenTile TheGreenTile
TheGreenMile TheGreenTile TheGreenMile TheGreenTile
TheGreenMile TheGreenTile TheGreenTile TheGreenTile
TheGreenTile TheGreenTile TheGreenTile TheGreenTile

I color coded it to help you see what's happening.

Oh yeah, the Boost regex docs helped me figure it out.
Stefan
Posted on: 10/17/2011 9:46 pm
Home away from home
Joined: 7/14/2008
From: Germany, EU
Posts: 257
Re: Example please for (?n:true_expression:false_expression)

Thanks John!

I see it, but i don't understand it.
Will have an closer look and read the Boost help...
Stefan
Posted on: 10/17/2011 11:18 pm
Home away from home
Joined: 7/14/2008
From: Germany, EU
Posts: 257
Re: Example please for (?n:true_expression:false_expression)
Quote:

Conditionals
The character '?' begins a conditional expression, the general form is:
?Ntrue-expression:false-expression
where N is decimal digit.
If sub-expression N was matched, then true-expression is evaluated and sent to output,
otherwise false-expression is evaluated and sent to output.
You will normally need to surround a conditional-expression with parenthesis in order to prevent ambiguities.
For example, the format string "(?1foo:bar)" will replace each match found with "foo" if the sub-expression $1 was matched,
and with "bar" otherwise.
For sub-expressions with an index greater than 9, or for access to named sub-expressions use:
?{INDEX}true-expression:false-expression
or
?{NAME}true-expression:false-expression


conditional expression ?Ntrue-expression:false-expression

In addition, conditional expressions of the following form are recognized:
?Ntrue-expression:false-expression
where N is a decimal digit representing a sub-match.
If the corresponding sub-match participated in the full match, then the substitution is true-expression.
Otherwise, it is false-expression. In this mode, you can use parens () for grouping.
If you want a literal paren, you must escape it as \(.






Seams clear, but i have still problems using this.
Some tests:



conditional expression 
'?Ntrue-expression:false-expression' test 1

I have:
Test Price 1000
Test Price 100
Test Price 800
---
Find: (.+) (.+) (\d{3})(\d)
Replace: (?4:\0 too expensive:\0 affordable)
[X] Use RegEx
---
Result:
Test Price 1000 too expensive
Test Price 100
Test Price 800
---
Expected:
Test Price 1000 too expensive
Test Price 100 affordable
Test Price 800 affordable
---
Explanation: 
If sub-expression No. 4 match THEN 
use 'true-expression' 
else 'false-expression'






conditional expression 
'?Ntrue-expression:false-expression' test 2

I have:
Color 1 green
Color 2 blue
Color 3 red
The available colors are either green, blue or red.
---
Find: (Color \d) (.+)
Replace: (?1:\1 \2-ich:\1 -\2-)
[X] Use RegEx
---
Result:
Color 1 green-ich
Color 2 blue-ich
Color 3 red-ich
The available colors are either green, blue or red.
---
Expected:
Color 1 green-ich
Color 2 blue-ich
Color 3 red-ich
The available colors are either -green-, -blue- or -red-.
---
Explanation: 
Only if sub-expression No. 1 will match THEN
use 'true-expression'
else 'false-expression'



OK, i will test some more.
JohnQSmith
Posted on: 10/18/2011 6:39 am
Not too shy to talk
Joined: 7/15/2011
From:
Posts: 30
Re: Example please for (?n:true_expression:false_expression)
I see the problem with both of your tests. It's the same thing that took me so long to figure out how it works.

Here's the key...
The whole RegEx must match in order for it to work. In other words, you have to set up the RegEx with an alternation so that it has both a success and a failure point.

In your first test, only the first line matched your RegEx (I'm using underscores as filler to demonstrate).


Test Price 100______0
(.+) (.+)  (\d{3}) (\d)   <-- this matches

Test Price 100________
(.+) (.+)  (\d{3}) (\d)   <-- this doesn't match, there is no final (\d)

Test Price 800________
(.+) (.+)  (\d{3}) (\d)   <-- also doesn't match

Here's how I changed your RegEx to work.

        Note the success ----.  and failure points
                             |  |
                             v  v
Find: ^(.+) (.+) (\d{3})(?:(\d)|$)
Replace: \0 (?4:too expensive:affordable)

Your second example is the same thing.

Color_ 1__ green
(Color \d) (.+)   <-- match

Color_ 2__ blue
(Color \d) (.+)   <-- match

Color_ 3__ red
(Color \d) (.+)   <-- match

The available colors are either green, blue or red.
(Color \d) (.+)   <-- no match anywhere on line

Hope this helps.
Stefan
Posted on: 10/18/2011 10:12 am
Home away from home
Joined: 7/14/2008
From: Germany, EU
Posts: 257
Re: Example please for (?n:true_expression:false_expression)
Thank you.

My understanding right now:
You have to set up the FIND RegEx with an alternation so that it has both a success and a failure point.
As the REPLACE have too possibilities too: (?n:true_expression:false_expression)


Test:

I have:
Test Price 1000
Test Price 100
Test Price 800

Find: (.+) (.+) (\d{3})(\d)*
Repl: (?4:\0 too expensive:\0 affordable)
[X] Use RegEx

Result:
Test Price 1000 too expensive
Test Price 100 affordable
Test Price 800 affordable




Thanks again. I still have to do some test, but i think you showed me the way.



-
BTW, good idea of you:
Replace: \0 (?4:too expensive:affordable)
-
JohnQSmith
Posted on: 10/18/2011 10:39 am
Not too shy to talk
Joined: 7/15/2011
From:
Posts: 30
Re: Example please for (?n:true_expression:false_expression)
Quote:
My understanding right now:
You have to set up the FIND RegEx with an alternation so that it has both a success and a failure point.
As the REPLACE have too possibilities too: (?n:true_expression:false_expression)

Absolutely correct. I like your FIND RegEx better. It will be much easier to use than mine. Just adding the zero or more switch makes it much simpler than a non-matching alternation grouping.

What helped me the most in figuring out how it works was how EmEditor highlights all matches when you do a search. When I tried your first search, only the first "Test Price" line was highlighted, but when I removed the final "(\d)", all the lines were marked. This showed me that the problem was with the RegEx.

The first and foremost thing to remember is that the WHOLE RegEx expression must match before you can do any further matching and testing with a SUBexpression.

Quote:
BTW, good idea of you:
Replace: \0 (?4:too expensive:affordable)

Thanks!
Threaded | Newest First Previous Topic | Next Topic | Top


Register To Post
 
English čeština Deutsch español français italiano 日本語 한국어 Русский 简体中文 繁體中文