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

Flat Previous Topic | Next Topic
Poster Thread
Stefan
Posted on: 10/17/2011 11:18 pm
Home away from home
Joined: 7/14/2008
From: Germany, EU
Posts: 263
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.
Flat Previous Topic | Next Topic


Subject Poster Date
   Example please for (?n:true_expression:false_expression) Stefan 10/13/2011 10:51 pm
     Re: Example please for (?n:true_expression:false_expression) JohnQSmith 10/17/2011 12:10 pm
       Re: Example please for (?n:true_expression:false_expression) Stefan 10/17/2011 9:46 pm
       » Re: Example please for (?n:true_expression:false_expression) Stefan 10/17/2011 11:18 pm
           Re: Example please for (?n:true_expression:false_expression) JohnQSmith 10/18/2011 6:39 am
             Re: Example please for (?n:true_expression:false_expression) Stefan 10/18/2011 10:12 am
               Re: Example please for (?n:true_expression:false_expression) JohnQSmith 10/18/2011 10:39 am

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