Issues with quotes in strings

Welcome Forums General PowerShell Q&A Issues with quotes in strings

This topic contains 2 replies, has 3 voices, and was last updated by

4 months, 3 weeks ago.

  • Author
  • #126759

    Points: 73
    Rank: Member

    I am working on a PowerShell GUI. For one of the fields I want to lock it down to only numbers and periods, for IP address input. After several attempts at regex I ended up cobbling this together:

        $IPInput = New-Object System.Windows.Forms.TextBox
            $IPInput.Location = New-Object System.Drawing.Point(445, 60)
            $IPInput.Size = New-Object System.Drawing.Size(170, 20)
            $IPInput.TextAlign = 'Center'
            $IPInput.MaxLength = 15
                if($IPInput.Text -match '[A-Z!@#$%^&*();;_=+< ,>?/|\`"`~{[}}-]') {
                    $IPInput.Text = $IPInput.Text -replace '[A-Z!@#$%^&*();;_=+< ,>?/|\`"`~{[}}-]'
                        if($IPInput.Text.Length -gt 0) {
                            $IPInput.SelectionStart = $IPInput.Text.Length

    This works for every character except a single quote ('). I have tried putting a ` before the quote. Changing the enclosing quotes to "" and then adding the single quote character, and about every other permutation I could think of. Can anyone suggest the best method for including the single quote, or an easy way to exclude anything and everything except numbers and periods?

  • #126765

    Points: 639
    Helping Hand
    Rank: Major Contributor

    There should be a ton of examples of a regex for IP addresses, but what you are using is not at first glance, because nothing is ensuring that you are entering a valid octet (e.g.255). Take a look at this post which uses this regex:

  • #126782

    Points: 1,990
    Helping Hand
    Rank: Community Hero

    How about,

    $IPAddress = ''

    You just need to makes sure $IPAddress.Split('\.').count -eq 4

The topic ‘Issues with quotes in strings’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort