Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

I neeed regex to retrieve a macine name

Last post 03-08-2010, 7:22 PM by Aussie Susan. 2 replies.
Sort Posts: Previous Next
  •  03-05-2010, 6:17 AM 60400

    I neeed regex to retrieve a macine name

    Hi, I am using Autoit to connect client to server.

    The client user supposed to select a machine name in order to connect to the server.

    The browse dialoge return the following string if a string contain a machine name:

     

    \\Host\folder1\folder2

     I expect from the regex to retrieve only the \\Host String.

     

    can you help please.

     

     

  •  03-06-2010, 4:13 PM 60423 in reply to 60400

    Re: I neeed regex to retrieve a macine name

    Please read the positing guidelines : http://regexadvice.com/forums/thread/58291.aspx    IT IS IMPOSSIBLE TO WRITE A REGULAR EXPRESSION IF THE INPUT DATA IS UNKNOWN.

     e.g. what is the input string. What characters does it use ? Is it a UNC pathname ? Do you require a min/max length limit to the name you want to use ? Is it a name already validated or a user input that could contain malicious sequences ? Is \\?\ something you expect to filter ? is \\.\ something you expect to filter ? is \\ftp: something you expect to filter? Are your function responsible for provising a valid name to the application ? Or someone else will validate the name ? And so on ...  I WARN YOU : there is no way to separate a server name from the protocol and from the folders in the general case. Only functions provided by the OS would guarantee you that what you are doing is correct.

    The regular expression for autoit seems to be documented here, looks to be pcre commpliant http://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm

    Build your regular expression as follows

    ^      match the beginning of the buffer

    \\\\    match the 2 first \ \

    [^\\]+    any byte, but not a \  , repeated at least 1 time

    \\    the first \ after the host

    .* anything after

     

    Concatenate the whole lot to get the complete regular expression

    ^(\\\\[^\\]+)\\.*

    Then if the input is

      \\Host\folder1\folder2

    $array = StringRegExp(.....) would return the following string in $array[0]

      \\Host

    Please read carefully the beginning of this mail, this expression will NOT guarantee that the name is a valid server name . this regular expression matches only YOUR descriptioon of the problem.

     

  •  03-08-2010, 7:22 PM 60499 in reply to 60423

    Re: I neeed regex to retrieve a macine name

    Just a quick question: by adding the '\\.*' at the end, you are going to include all remaining characters in the input string in the eventual match. Normally the entire match is captured in the "zeroth" match group. Should you be looking at "$array[1]" for just the host name?

    The alternative is to leave off the '\\.*' as the first part has already found everything necessary (ie all characters that are not a backslash). In this case, the '$array[0]' should be fine.

    Or is there something a bit different about "autoit"?

    Susan

View as RSS news feed in XML