Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2015-09

use of function ExtractWord

5 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

John Landmesser

Hi,

Example:

given a string:

str := '2015-09-14 14:46:50  downloadrate  (813 KB/s)'

if i want to get the first word ( worddelim is space ) i use:

Result := ExtractWord(1 ,str,['  ']);

Result is '2015-09-14'

That's clear :-))

But!

If i want to extract '(813 KB/s)', i have to extract the SECOND word!!

Result := ExtractWord(2 ,str,['( ', ')']);

Result is '813 KB/s'

My question:

Why do i have to extract the SECOND word? The word inside () occurs just once!

Delphi 5 delivers the same Result so i'm sure i misunderstand something?!!

lhelp tells me:

Show quoted text 7 lines
ExtractWord extracts the N-th word from the string S. The set of characters in WordDelims are used to mark word boundaries. A word is defined as any non-empty sequence of characters which are not present in WordDelims: if a character is not in WordDelims, it is considered as part of a word. If an N-th word cannot be found, an empty string is returned.

Explanations are welcome.

Thanks!!

Reply by email

Michael Van Canneyt
to John Landmesser
John Landmesser wrote 28 lines
Hi, Example: given a string: str := '2015-09-14 14:46:50 downloadrate (813 KB/s)' if i want to get the first word ( worddelim is space ) i use: Result := ExtractWord(1 ,str,[' ']); Result is '2015-09-14' That's clear :-)) But! If i want to extract '(813 KB/s)', i have to extract the SECOND word!! Result := ExtractWord(2 ,str,['( ', ')']); Result is '813 KB/s' My question: Why do i have to extract the SECOND word? The word inside () occurs just once!

Because you cannot specify separate begin and end separator, just 'separators'. ( is a separator, so everything before ( is the first word. if you specify ( ) as separators, there are 3 positions in the text: before (, between ( and ) and after ).
(the last one is empty)

Michael.

Reply by email

Graeme Geldenhuys
to John Landmesser: Example: given a string: str := '2015-09-14 14:46:50 downloadrate (813 KB/s)' if i want…
John Landmesser wrote
If i want to extract '(813 KB/s)', i have to extract the SECOND word!!

Michael already explained it.

I would rather opt to use regex to extract the exact piece of text you are interested in. FPC includes a capable regex implementation.

regex string: ?\((\d+\w.*/s)\)?

and you should be able to reference the value between '(' and ')' with the first matching group... normally referenced as ?\1?

ps:
The text between ? and ? are the regex syntax.

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/

Reply by email

John Landmesser
to Michael Van Canneyt: Because you cannot specify separate begin and end separator, just 'separators'. ( is a…

got it, thank you very much!!

Michael Van Canneyt wrote 15 lines
Because you cannot specify separate begin and end separator, just 'separators'. ( is a separator, so everything before ( is the first word. if you specify ( ) as separators, there are 3 positions in the text: before (, between ( and ) and after ). (the last one is empty) Michael. -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email