2

i have text like (with new line etc.), eg.:

[my_tag]foo[/my_tag]
  bar baz [my_tag]bar 
foo[/my_tag] 
foo [my_tag]bar baz
foo[/my_tag] 

i want totally remove all [my_tag][/my_tag] content, eg.:

[my_tag][/my_tag]
  bar baz [my_tag][/my_tag] 
foo [my_tag][/my_tag] 

my code not work (it seems not match newline):

var
  aPerlRegEx : TPerlRegEx;
  aContent   : string;
begin
  aContent := '';
  aContent := aContent + '[my_tag]foo[/my_tag]' + #13#10;
  aContent := aContent + 'bar baz [my_tag]bar ' + #13#10;
  aContent := aContent + 'foo[/my_tag] '        + #13#10;
  aContent := aContent + 'foo [my_tag]bar baz'  + #13#10;
  aContent := aContent + 'foo[/my_tag] '        + #13#10;

  aPerlRegEx := TPerlRegEx.Create;
  try
    with aPerlRegEx do begin
       Options     := [preCaseLess, preMultiLine];
       RegEx       := '\[my_tag\].*?\[\/my_tag\]';
       Subject     := aContent;
       Replacement := '';
    end;

    if aPerlRegEx.Match then
        aPerlRegEx.ReplaceAll;

    writeln(aPerlRegEx.Subject);
  finally
    FreeAndNil(aPerlRegEx);
  end;
4
  • Can these tags be nested? Commented May 5, 2016 at 10:56
  • @WiktorStribiżew no.. Commented May 5, 2016 at 10:57
  • Then you must add the preSingleLine flag. And remove preMultiLine Commented May 5, 2016 at 10:59
  • yes @WiktorStribiżew Commented May 5, 2016 at 13:09

1 Answer 1

2

Since you say these tags cannot be nested, you can use your regex

\[my_tag\].*?\[\/my_tag\]

With the preSingleLine flag to make the . match newline symbols. See the regex demo (s option is used at the demo site).

Note that you can safely remove preMultiLine since you do not have ^ and $ in yout pattern to redefine the behavior of.

Or, you can unroll the lazy dot matching pattern for better performance:

\[my_tag\][^[]*(?:\[(?!\/my_tag\])[^[]*)*\[\/my_tag\]

See another regex demo. You do not have to use preSingleLine flag then as the negated character class [^[] match newlines, too.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.