0

I would like to capture dynamic gateway IP populated in a memo to a text box. How can I do it with either wildcards or capture the whole line of gateway that starts with "Gateway: 10.127.*.*" and get the gateway IP to the text box.

Here is the example of memo text already captured:

Description: Microsoft Hosted Network Virtual Adapter
HW Address Length: 6
HWAddress: E0:2A:82:F9:B2:3E
Index: 32
Type: 71
CurrentIPAddress: 
IP Addresses:    192.168.48.1/255.255.255.0
Gateway:    0.0.0.0/255.255.255.255
Name: {22712B8F-5E3A-48D4-8C0D-771708BF0305}
Description: HUAWEI Mobile Connect - Network Card
HW Address Length: 6
HWAddress: 0C:5B:8F:27:9A:64
Index: 4
Type: 243
CurrentIPAddress: 
IP Addresses:    10.127.144.193/255.255.255.252
Gateway:    10.127.144.194/255.255.255.255

EDIT: (from Comment)

I:= Pos('Gateway: 10.127.', Memo1.Text);
if I > 0 then begin
   L := SendMessage(Memo1.Handle, EM_LINEFROMCHAR,   (intTostr(1));
   edit1.Text:=(intTostr(L)); 
Michael
  • 41,989
  • 11
  • 82
  • 128
Jk Robbin
  • 65
  • 10
  • 2
    What version of Delphi? Recent versions support regular expressions that can be used to help here; earlier versions don't. – Ken White Aug 23 '16 at 12:37
  • What part of this do you find challenging? Why are you using a GUI control for text processing? What does your code look like? Are you hoping we will write your code for you? – David Heffernan Aug 23 '16 at 13:00
  • i have a sample code but only gets me line number – Jk Robbin Aug 23 '16 at 13:09
  • `I:= Pos('Gateway: 10.127.', Memo1.Text); if I > 0 then begin L := SendMessage(Memo1.Handle, EM_LINEFROMCHAR,(intTostr(1); edit1.Text:=(intTostr(L));` – Jk Robbin Aug 23 '16 at 13:09
  • 2
    There appear to be 3 spaces following the `:`. And why on earth are you using `EM_LINEFROMCHAR`? That's insane. You are text processing. No place for GUI controls. Do you know how to debug your code? If not then everything else is pointless. Stop what you are doing and learn to debug. – David Heffernan Aug 23 '16 at 13:12

2 Answers2

3

This is a Delphi 7 version of René's answer:

var
  ii: integer
begin
  for ii := 0 to memo.lines.count -1 do begin
    if pos('Gateway:   10.127.', memo.lines[ii]) > 0 then begin
      textbox.caption := memo.lines[ii];
      break; 
    end;
  end;
end;
Michael Vincent
  • 1,620
  • 1
  • 20
  • 46
  • thanks a lot @Micheal Vicent it works well its capture How do i remove the Gateway: word and only remain with the result at `10.127.` – Jk Robbin Aug 23 '16 at 13:36
  • 6
    @JkRobbin How do you think it would be done? Have you really given up all hope of solving this yourself? This is simple text processing. Don't ask new questions in comments. Why not find a tutorial or text book or even the documentation. Surely you aren't the first person to want to extract a substring? Are you sure you can't work this out for yourself? I know I sound harsh, but you are simply not trying hard enough, and this site is not intended to teach novices how to do basic tasks. Please visit the [help]. We are here to build a resource of Q&A posts that will be useful to future visitors. – David Heffernan Aug 23 '16 at 13:45
  • 2
    only for completeness :You can not trust the spaces or signs in a created and captured text ! Here is, not 100 %, but better : `if pos('GATEWAY:10.127.', UpperCase(StringReplace(Memo1.lines[ii],' ','',[rfReplaceAll]))) > 0 then` – moskito-x Aug 23 '16 at 16:22
  • @moskito - nice addition. Regards, – Michael Vincent Aug 24 '16 at 07:54
1

Easist way to achieve this would be to iterate over the memo's lines and check if it begins with "Gateway: 10.127.".

For example:

for LString in memo.Lines do
  if AnsiStartsText('Gateway:    10.127.', LString) then
    textbox.Caption := LString

You could use regular expressions as well for more specific handling.

René Hoffmann
  • 2,766
  • 2
  • 20
  • 43
  • 3
    Delphi 7 doesn't have the for .. in loop, nor regular expressions that were suggested as possibly helping. You'll need to use a for loop with an index, a bit like: for I := 0 to memo.lines.count -1 do – Michael Vincent Aug 23 '16 at 13:07