4

How can i extract randomstring between A & B. For example:

A randomstring B

7
  • 3
    I cannot speak for others, but I have problems to understand what you want. Can you give a concrete example? Commented Dec 31, 2012 at 9:11
  • @UweRaabe This should make it more clear :) Strings not chars! :) Commented Dec 31, 2012 at 9:13
  • BTW, one answer will be "regular expressions" because the question contains "extract" and "strings" ;) Commented Dec 31, 2012 at 9:13
  • @UweRaabe Kein problem ;) Commented Dec 31, 2012 at 9:14
  • 1
    I don't understand your question either. Give a concrete example please. Commented Dec 31, 2012 at 9:17

3 Answers 3

18

Assuming that "randomstring" doesn't contain the enclosing strings "A" or "B", you can use two calls to pos to extract the string:

function ExtractBetween(const Value, A, B: string): string;
var
  aPos, bPos: Integer;
begin
  result := '';
  aPos := Pos(A, Value);
  if aPos > 0 then begin
    aPos := aPos + Length(A);
    bPos := PosEx(B, Value, aPos);
    if bPos > 0 then begin
      result := Copy(Value, aPos, bPos - aPos);
    end;
  end;
end;

The function will return an empty string when either A or B are not found.

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

3 Comments

Kudos for answering Uwe, because I still don't understand the question ;-)
I have a recursive version of this which populates a String Array... useful for extracting (for example) all Hyperlinks on a web page's source.
Note: PosEx comes from StrUtils
4

Answer with regular expression :)

uses RegularExpressions;
...
function ExtractStringBetweenDelims(Input : String; Delim1, Delim2 : String) : String;


var
  Pattern : String;
  RegEx   : TRegEx;
  Match   : TMatch;

begin
 Result := '';
 Pattern := Format('^%s(.*?)%s$', [Delim1, Delim2]);
 RegEx := TRegEx.Create(Pattern);
 Match := RegEx.Match(Input);
 if Match.Success and (Match.Groups.Count > 1) then
  Result := Match.Groups[1].Value;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 ShowMessage(ExtractStringBetweenDelims('aStartThisIsWhatIWantTheEnd', 'aStart', 'TheEnd'));
end;

3 Comments

@NunoJemaio: the nice thing is that the same pattern would also work for case insensitive search (just add option to TRegEx.Create)
Will your method work if either the delim is = "" ? For example at the front or end of url's. This works perfect for middle string matching where both delims are not ""
This should have been the winning answer. I am stripping tags out of several different brackets and this worked perfectly. Made up a quick demo, from start to a working project, about 5 minutes. THANK YOU!!! Gotta love RegEx.
3

Another approach:

function ExtractTextBetween(const Input, Delim1, Delim2: string): string;
var
  aPos, bPos: Integer;
begin
  result := '';
  aPos := Pos(Delim1, Input);
  if aPos > 0 then begin
    bPos := PosEx(Delim2, Input, aPos + Length(Delim1));
    if bPos > 0 then begin
      result := Copy(Input, aPos + Length(Delim1), bPos - (aPos + Length(Delim1)));
    end;
  end;
end;

Form1.Caption:= ExtractTextBetween('something?lol/\http','something?','/\http');

Result = lol

1 Comment

This might fail when Delim2 appears before or inside Delim1. If you can exclude this situation, your solution works, too. Btw: Str is the name of a built-in procedure and should be avoided for variable names.

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.