andgineer / TRegExpr

Regular expressions (regex), pascal.
https://regex.sorokin.engineer/en/latest/
MIT License
174 stars 63 forks source link

Replace isn't working as it should be #163

Closed SlMaker closed 4 years ago

SlMaker commented 4 years ago

1.

now last RE test fails. Test44. why? it calls finder, then calls Replace. finder works ok. Replace (it calls Substitute) gets whole string as result - it is some bug. I didnt find it. yet

Originally posted by @Alexey-T in https://github.com/andgineer/TRegExpr/issues/155#issuecomment-632668019

2.

maybe ALL replace-tests are wrong. see this:

// 2
(
expression: '(\w*)';
inputText: 'name.ext';
substitutionText: '$1.new';
expectedResult: 'name.new.new.ext.new.new';
MatchStart: 0
),

Originally posted by @Alexey-T in https://github.com/andgineer/TRegExpr/issues/155#issuecomment-632673989

Testing it on https://regex101.com/r/HblhQF/1 gives also 4 matches: name, empty, ext, empty

name match: name.new empty match: .new ext match: ext.new empty match: .new but the dot between new and ext shouldn't be there?

andgineer commented 4 years ago

yes - replace leave intact all parts of the input text that are not matched by re - see doc https://regex.sorokin.engineer/en/latest/tregexpr.html#replace-replaceex

SlMaker commented 4 years ago

But it doesn't make sense for

    // valid regex set
    expression: '([.-]Test[.-])';
    inputText: 'This.Is.A.Test.1234_Test_abc';
    substitutionText: '$1';
    expectedResult: '.Test.';
    matchStart: 0

Why does it fail then?

from docu:

Expression := '((?i)block|var)\s(\s([^ ])\s)\s*'; Replace ('BLOCK( test1)', 'def "$1" value "$2"', True); Returns def "BLOCK" value "test1"

andgineer commented 4 years ago

it match .Test. and replace it with whole match - the same text. and leave all others intact.

so result should be the same as input string

may be better re would be [.-](Test)[.-] and result will be This.Is.ATest1234_Test_abc