Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #9732
    Stefan
    Participant

    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.

    #9741
    JohnQSmith
    Participant

    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.

    #9742
    Stefan
    Participant

    Thanks John!

    I see it, but i don’t understand it.
    Will have an closer look and read the Boost help…

    #9743
    Stefan
    Participant

    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: too expensive: 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.

    #9744
    JohnQSmith
    Participant

    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: (?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.

    #9745
    Stefan
    Participant

    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: too expensive: 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 :lol: of you:
    Replace: (?4:too expensive:affordable)

    #9746
    JohnQSmith
    Participant

    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.

    BTW, good idea :lol: of you:
    Replace: (?4:too expensive:affordable)

    Thanks! :-D

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.