2

I have a text that is a source-code file, I need to find the first parameter of all calls of this function. That can be called in this 2 ways. I'm completely dumb in regex.

lang('FOUND') and lang('FOUND',[loremipsum])

FOUND should be anything, is this value that I'm searching.

I will execute the Regexp in delphi, but I think regular expression is language independent

EDIT


Using the @Ken White answer and this Video I did this code, and works fine!

Regular expression: (?:lang\s*\(\s*')(.*?)(?:'\s*\)|'\s*\,\s*\[.*\]\s*\))

Include the Unit PerlRegEx on Uses

procedure TForm1.Button1Click(Sender: TObject);
var
  Regex: TPerlRegEx;
begin

  Regex := TPerlRegEx.Create();
  Regex.RegEx := '(?:lang\s*\(\s*'')(.*?)(?:''\s*\)|''\s*,\s*\[.*\]\s*\))';
  Regex.Options := [preCaseless, preMultiLine];
  Regex.Subject := Memo1.Text;
  if Regex.Match then
  begin
    repeat

      Memo2.Lines.Add( Regex.Groups[1] );

    until not Regex.MatchAgain;
  end;

end;
5
  • Regexp is not language independent. Commented Nov 27, 2011 at 4:51
  • 1
    Ummm... What? Try posting again without all the "loremipsum" and shading and bold stuff, put in actual text you're using to try and search, and an example of what you're trying to match, and what you've tried so far that isn't working, and perhaps someone can help. Right now, it's just a bunch of noise. We can't read your mind to know what you're trying to do; the more clear you can make your question, the better the chances are of getting an answer. :) Commented Nov 27, 2011 at 4:53
  • @KenWhite I simplified the question. Commented Nov 27, 2011 at 5:23
  • Ah, got it. You're the one insisting on trying to do your own internationalization code. :) Are you always searching for something following lang that's surrounded by a quoted string in parentheses like lang(' followed by some text until a closing )? Commented Nov 27, 2011 at 6:01
  • 1
    remember to expect regex based source code parsing to fail from time to time Commented Nov 27, 2011 at 9:23

1 Answer 1

2

(All of the code below was generated by Regex Buddy; I also used it to create the regex used. SubjectString is the content of your source file, loaded into a string; you can load the source file into a stringlist, for instance, and pass the Text.)

Using TPerlRegex, available from the RegEx Buddy website, you can use

var
  Regex: TPerlRegEx;
begin
  ...
  Regex := TPerlRegEx.Create(nil);
  Regex.RegEx := 'lang\((.*)\)';
  Regex.Options := [preMultiLine];
  Regex.Subject := SubjectString;
  if Regex.Match then 
  begin
    repeat
      // matched text: Regex.MatchedExpression;
      // match start: Regex.MatchedExpressionOffset;
      // match length: Regex.MatchedExpressionLength;
      // backreference n text: Regex.SubExpressions[n];
      // backreference n start: Regex.SubExpressionOffsets[n];
      // backreference n length: Regex.SubExpressionLengths[n];
    until not Regex.MatchAgain;
  end;
end;

Using Delphi XE's regex support (use Regex), you can use

var
  Regex: TRegEx;
  MatchResults: TMatch;
begin
    ...
  try
    Regex := TRegEx.Create('lang\((.*)\)', [roMultiLine]);
    MatchResults := Regex.Match(SubjectString);
    while MatchResults.Success do begin
      MatchResults := MatchResults.NextMatch();
    end;
  except
    on E: ERegularExpressionError do 
    begin
      // Syntax error in the regular expression
    end;
  end;
end;

In both cases, the text you want (the part between the parentheses) will be in capturing group 1 of each match.

I seem to recall from other questions that you're using Delphi 7, so the first will probably be the best option.

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

1 Comment

Thank You very much! I checked your answer as accepted, and edited my question. my internalization code now is done, I used this to generate a string list, iterating all source-code files on the project folder.

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.