Why No Built in GetMyIP call in LiveCode?

Jim Ault jimaultwins at yahoo.com
Fri Jun 10 21:21:37 EDT 2011

> How does the matchText call know just to capture the IP address in  
> the variable retVal?

Okay, you said you wanted to know what is going on, so here it is.

Modifier at the beginning of a statement
says that the '.' char CAN match a return char, thus the match can  
span 2 or more lines
The 's' in this case does not mean 'space char' or the literal 's' char.

When the scanning begins and the 'inet ' portion is found to match,  
then the next condition is tried

The () parens are telling the engine to capture any chars that meet  
the conditions inside and assign them to the first variable  
specified.  In this case, it is 'retVal'
If there were a second set of (), then those chars would be assigned  
to the second variable specified.

So the first ? has nothing to do with space chars.
   it is an operator that affects the logic of the scan
    depending on the chars inside the parens (?s)

And the second ? has nothing to do with space chars.
It is a different operator that says to the (.*) scan to be UN-greedy.
This means, return the shortest version of a match, rather than the  
longest possible one.

The longest match would be "addr:  Bcast:"
- because the logic is 'find the longest match that allows a string  
that ends in a space'.

Note carefully that the regex ends with    (.*?) "     < a trailing  
If the ? was omitted, then the longest match that can end in a space  
char would be the string that includes Bcast.

We don't want the longest string, so we need to tell regex either
              (?s)        (.*?)
-- or --
              (?Us)     (.*)
-- both say 'ungreedy' or 'shortest match'

Both return a string that only includes the chars between the (), so
both return "addr:" without a trailing space.
Final spec is to find a string that begins with "inet "
and ends with a single space, then
return the chars after the first space and before the last space, and
make it the shortest string you can find, not the longest.
Oh, and the (?s) says that the shortest string can include return chars.
In this case, we are working with a two-line value where every char is  
on the second line, so the (?s) is not essential, but does no harm.

--******-- variation you might want to use
This ungreedy regex would return retVal = only the ip address

put "(?U)inet (.*):(.*)" & space into regEx
put \
      matchText(tEthernetConfig, regEx, ipLabel, retVal) \
       into wasSuccessfulTrueOrFalse

Note the ':' separator & the extra variable we don't use 'ipLabel'
   ipLabel = "addr"
   retVal = ''

Hope this is crystal clear on a Friday night.
Regex is an xtalk language where the x stands for extraterrestrial.

Jim Ault
Las Vegas

On Jun 10, 2011, at 4:12 PM, John Patten wrote:

> I settled on Mark's shell script process to get IP address. I  
> managed to kludge together a case for Linux (Ubuntu Netbook) too. I  
> did not quite understand the matchtext line with its (?s) and  
> (.*?)'s but the Mac OSX case and Linux case almost worked without  
> any changes.
> The Linux shell is same as Mac's: ifconfig with either eth5 or wlan3  
> for interfaces.
> The shell command results are:
> 		wlan3     Link encap:Ethernet  HWaddr 00:25:d3:7c:f2:49
>          	inet addr:  Bcast:  Mask:
> The only difference between the two is there are the characters  
> "addr:" in front of the IP with no space too.  The OSX shell has  
> spaces surrounding the IP.
> Mark had mentioned that the (?s) represent spaces.
> How does the matchText call know just to capture the IP address in  
> the variable retVal?  It appears to look for "<space>inet<space>"  
> and when it finds it magically captures just the IP address that  
> follows it???? How's it do that?  Does matchText automatically get  
> the first item/word following the matched text??? Not quite sure how  
> that works, but it does :-)
> --------------------snip----------------
> break
> 		  case "Linux"
>                        put shell("/sbin/ifconfig eth5") into  
> tEthernetConfig
>                        put shell("/sbin/ifconfig wlan3") into  
> tWirelessConfig
>                   set the itemdel to "."
>                   get matchText(tEthernetConfig,"(?s)inet (.*?)  
> ",retVal)
>                   if it is false then
>                           -- CHECK FOR WIRELESS CONNECTION
>                           get matchText(tWirelessConfig,"(?s)inet  
> (.*?) ",retVal)
>                           if it is false then
>                                   return ""
>                           end if
>                   end if
>         -- i have no clue how the matchtext works above but the  
> retVal is returns "addr:10.X.X.X". Sooo...
>  set the itemDel to ":"
>          put item 2 of  retVal into cd fld "ipaddress"
>          end switch
>        return ""

More information about the Use-livecode mailing list