Regular Expressions in powershell split












3















I need to strip out a UNC fqdn name down to just the name or IP depending on the input.



My examples would be



\tom.overflow.corp.com
\123.43.234.23.overflow.corp.com


I want to end up with just tom or 123.43.234.23



I have the following code in my array which is striping out the domain name perfect, but Im still left with \tom



-Split '.(?!d)')[0]









share|improve this question

























  • have you tried regex101.com?

    – Kory Gill
    Nov 27 '18 at 21:17











  • @Kory Gill thank you ,nice website, I will use it more. Still have not found my solution.

    – jim wood
    Nov 27 '18 at 22:13
















3















I need to strip out a UNC fqdn name down to just the name or IP depending on the input.



My examples would be



\tom.overflow.corp.com
\123.43.234.23.overflow.corp.com


I want to end up with just tom or 123.43.234.23



I have the following code in my array which is striping out the domain name perfect, but Im still left with \tom



-Split '.(?!d)')[0]









share|improve this question

























  • have you tried regex101.com?

    – Kory Gill
    Nov 27 '18 at 21:17











  • @Kory Gill thank you ,nice website, I will use it more. Still have not found my solution.

    – jim wood
    Nov 27 '18 at 22:13














3












3








3








I need to strip out a UNC fqdn name down to just the name or IP depending on the input.



My examples would be



\tom.overflow.corp.com
\123.43.234.23.overflow.corp.com


I want to end up with just tom or 123.43.234.23



I have the following code in my array which is striping out the domain name perfect, but Im still left with \tom



-Split '.(?!d)')[0]









share|improve this question
















I need to strip out a UNC fqdn name down to just the name or IP depending on the input.



My examples would be



\tom.overflow.corp.com
\123.43.234.23.overflow.corp.com


I want to end up with just tom or 123.43.234.23



I have the following code in my array which is striping out the domain name perfect, but Im still left with \tom



-Split '.(?!d)')[0]






regex powershell powershell-v2.0 powershell-v3.0 regex-lookarounds






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 23:23









Kory Gill

5,4861926




5,4861926










asked Nov 27 '18 at 21:09









jim woodjim wood

343




343













  • have you tried regex101.com?

    – Kory Gill
    Nov 27 '18 at 21:17











  • @Kory Gill thank you ,nice website, I will use it more. Still have not found my solution.

    – jim wood
    Nov 27 '18 at 22:13



















  • have you tried regex101.com?

    – Kory Gill
    Nov 27 '18 at 21:17











  • @Kory Gill thank you ,nice website, I will use it more. Still have not found my solution.

    – jim wood
    Nov 27 '18 at 22:13

















have you tried regex101.com?

– Kory Gill
Nov 27 '18 at 21:17





have you tried regex101.com?

– Kory Gill
Nov 27 '18 at 21:17













@Kory Gill thank you ,nice website, I will use it more. Still have not found my solution.

– jim wood
Nov 27 '18 at 22:13





@Kory Gill thank you ,nice website, I will use it more. Still have not found my solution.

– jim wood
Nov 27 '18 at 22:13












4 Answers
4






active

oldest

votes


















2














Your regex succeeds in splitting off the tokens of interest in principle, but it doesn't account for the leading \ in the input strings.



You can use regex alternation (|) to include the leading \ at the start as an additional -split separator.



Given that matching a separator at the very start of the input creates an empty element with index 0, you then need to access index 1 to get the substring of interest.



In short: The regex passed to -split should be '^\\|.(?!d)' instead of '.(?!d)', and the index used to access the resulting array should be [1] instead of [0]:



'\tom.overflow.corp.com', '\123.43.234.23.overflow.corp.com' | 
ForEach-Object { ($_ -Split '^\\|.(?!d)')[1] }


The above yields:



tom
123.43.234.23




Alternatively, you could remove the leading \ in a separate step, using -replace:



'\tom.overflow.corp.com', '\123.43.234.23.overflow.corp.com' | 
ForEach-Object { ($_ -Split '.(?!d)')[0] -replace '^\\' }




Yet another alternative is to use a single -replace operation, which does not require a ForEach-Object call (doesn't require explicit iteration):



'\tom.overflow.corp.com', '\123.43.234.23.overflow.corp.com' -replace
'?(x) ^\\ (.+?) .D .+', '$1'



  • Inline option (?x) (IgnoreWhiteSpace) allows you to make regexes more readable with insignificant whitespace: any unescaped whitespace can be used for visual formatting.


  • ^\\ matches the \ (escaped with ) at the start (^) of each string.


  • (.+?) matches one or more characters lazily.


  • .D matches a literal . followed by something other than a digit (d matches a digit, D is the negation of that).


  • .+ matches one or more remaining characters, i.e., the rest of the input.


  • $1 as the replacement operand refers to what the 1st capture group ((...)) in the regex matched, and, given that the regex was designed to consume the entire string, replaces it with just that.







share|improve this answer


























  • Glad to hear it, @jimwood; my pleasure. As an aside: While I was personally notified of your comment due to being the author of this answer, the others who answered here were not.

    – mklement0
    Nov 30 '18 at 23:52



















2














I'm stealing Lee_Daileys $InSTuff
but appending a RegEx I used recently



$InStuff = -split @'
\tom.overflow.corp.com
\123.43.234.23.overflow.corp.com
'@
$InStuff |ForEach-Object {($_.Trim('\') -split '.(?!d{1,3}(.|$))')[0]}


Sample Output:



tom
123.43.234.23


As you can see here on RegEx101 the dots between the numbers are not matched






share|improve this answer

































    1














    The Select-String function uses regex and populates a MatchInfo object with the matches (which can then be queried).



    The regex "(.?d+)+|w+" works for your particular example.



    "\tom.overflow.corp.com", "\123.43.234.23.overflow.corp.com" |
    Select-String "(.?d+)+|w+" | % { $_.Matches.Value }





    share|improve this answer

































      0














      while this is NOT regex, it does work. [grin] i suspect that if you have a really large number of such items, then you will want a regex. they do tend to be faster than simple text operators.



      this will get rid of the leading \ and then replace the domain name with .



      # fake reading in a text file
      # in real life, use Get-Content
      $InStuff = -split @'
      \tom.overflow.corp.com
      \123.43.234.23.overflow.corp.com
      '@

      $DomainName = '.overflow.corp.com'

      $InStuff.ForEach({
      $_.TrimStart('\').Replace($DomainName, '')
      })


      output ...



      tom
      123.43.234.23





      share|improve this answer
























      • thank you. Not really what I am after, I don't want to add more code. Is '.(?!d)' not regex? I have got it to do almost what I want with that little bit of code. I will keep trying

        – jim wood
        Nov 27 '18 at 22:12











      • @jimwood - it doesn't seem like "adding more code" from what i can see. [grin] the code posted by LotPings is significantly longer than what i posted. plus, my code is easily understood - regex is not quite so obvious. ///// however, if you need SPEED, then the regex method is likely the way to go.

        – Lee_Dailey
        Nov 27 '18 at 22:45











      • My code will remove any trailing domain parts, while yours is fixed to a given one, so functionality counts not length. Nitpicking: mine is 75 chars/yours is ~100

        – LotPings
        Nov 27 '18 at 23:34






      • 1





        @LotPings - that was not intended as any criticism of your code. i'm sorry if it seemed so, tho. [blush] it was a criticism of the OPs rationale for rejecting mine. [grin] as i mentioned, regex is almost certainly going to be faster for a large data set - if the OP has such a need. ///// i presumed the OP gave only one domain for a reason other than "oops! i should have mentioned that will change". ///// as an aside, will your regex handle Tom.Dooley.Overflow.Corp.com? that is why i gave up on a regex ... i could not think of any way to handle the possible pre-domain parts. [blush]

        – Lee_Dailey
        Nov 28 '18 at 1:21













      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53508163%2fregular-expressions-in-powershell-split%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Your regex succeeds in splitting off the tokens of interest in principle, but it doesn't account for the leading \ in the input strings.



      You can use regex alternation (|) to include the leading \ at the start as an additional -split separator.



      Given that matching a separator at the very start of the input creates an empty element with index 0, you then need to access index 1 to get the substring of interest.



      In short: The regex passed to -split should be '^\\|.(?!d)' instead of '.(?!d)', and the index used to access the resulting array should be [1] instead of [0]:



      '\tom.overflow.corp.com', '\123.43.234.23.overflow.corp.com' | 
      ForEach-Object { ($_ -Split '^\\|.(?!d)')[1] }


      The above yields:



      tom
      123.43.234.23




      Alternatively, you could remove the leading \ in a separate step, using -replace:



      '\tom.overflow.corp.com', '\123.43.234.23.overflow.corp.com' | 
      ForEach-Object { ($_ -Split '.(?!d)')[0] -replace '^\\' }




      Yet another alternative is to use a single -replace operation, which does not require a ForEach-Object call (doesn't require explicit iteration):



      '\tom.overflow.corp.com', '\123.43.234.23.overflow.corp.com' -replace
      '?(x) ^\\ (.+?) .D .+', '$1'



      • Inline option (?x) (IgnoreWhiteSpace) allows you to make regexes more readable with insignificant whitespace: any unescaped whitespace can be used for visual formatting.


      • ^\\ matches the \ (escaped with ) at the start (^) of each string.


      • (.+?) matches one or more characters lazily.


      • .D matches a literal . followed by something other than a digit (d matches a digit, D is the negation of that).


      • .+ matches one or more remaining characters, i.e., the rest of the input.


      • $1 as the replacement operand refers to what the 1st capture group ((...)) in the regex matched, and, given that the regex was designed to consume the entire string, replaces it with just that.







      share|improve this answer


























      • Glad to hear it, @jimwood; my pleasure. As an aside: While I was personally notified of your comment due to being the author of this answer, the others who answered here were not.

        – mklement0
        Nov 30 '18 at 23:52
















      2














      Your regex succeeds in splitting off the tokens of interest in principle, but it doesn't account for the leading \ in the input strings.



      You can use regex alternation (|) to include the leading \ at the start as an additional -split separator.



      Given that matching a separator at the very start of the input creates an empty element with index 0, you then need to access index 1 to get the substring of interest.



      In short: The regex passed to -split should be '^\\|.(?!d)' instead of '.(?!d)', and the index used to access the resulting array should be [1] instead of [0]:



      '\tom.overflow.corp.com', '\123.43.234.23.overflow.corp.com' | 
      ForEach-Object { ($_ -Split '^\\|.(?!d)')[1] }


      The above yields:



      tom
      123.43.234.23




      Alternatively, you could remove the leading \ in a separate step, using -replace:



      '\tom.overflow.corp.com', '\123.43.234.23.overflow.corp.com' | 
      ForEach-Object { ($_ -Split '.(?!d)')[0] -replace '^\\' }




      Yet another alternative is to use a single -replace operation, which does not require a ForEach-Object call (doesn't require explicit iteration):



      '\tom.overflow.corp.com', '\123.43.234.23.overflow.corp.com' -replace
      '?(x) ^\\ (.+?) .D .+', '$1'



      • Inline option (?x) (IgnoreWhiteSpace) allows you to make regexes more readable with insignificant whitespace: any unescaped whitespace can be used for visual formatting.


      • ^\\ matches the \ (escaped with ) at the start (^) of each string.


      • (.+?) matches one or more characters lazily.


      • .D matches a literal . followed by something other than a digit (d matches a digit, D is the negation of that).


      • .+ matches one or more remaining characters, i.e., the rest of the input.


      • $1 as the replacement operand refers to what the 1st capture group ((...)) in the regex matched, and, given that the regex was designed to consume the entire string, replaces it with just that.







      share|improve this answer


























      • Glad to hear it, @jimwood; my pleasure. As an aside: While I was personally notified of your comment due to being the author of this answer, the others who answered here were not.

        – mklement0
        Nov 30 '18 at 23:52














      2












      2








      2







      Your regex succeeds in splitting off the tokens of interest in principle, but it doesn't account for the leading \ in the input strings.



      You can use regex alternation (|) to include the leading \ at the start as an additional -split separator.



      Given that matching a separator at the very start of the input creates an empty element with index 0, you then need to access index 1 to get the substring of interest.



      In short: The regex passed to -split should be '^\\|.(?!d)' instead of '.(?!d)', and the index used to access the resulting array should be [1] instead of [0]:



      '\tom.overflow.corp.com', '\123.43.234.23.overflow.corp.com' | 
      ForEach-Object { ($_ -Split '^\\|.(?!d)')[1] }


      The above yields:



      tom
      123.43.234.23




      Alternatively, you could remove the leading \ in a separate step, using -replace:



      '\tom.overflow.corp.com', '\123.43.234.23.overflow.corp.com' | 
      ForEach-Object { ($_ -Split '.(?!d)')[0] -replace '^\\' }




      Yet another alternative is to use a single -replace operation, which does not require a ForEach-Object call (doesn't require explicit iteration):



      '\tom.overflow.corp.com', '\123.43.234.23.overflow.corp.com' -replace
      '?(x) ^\\ (.+?) .D .+', '$1'



      • Inline option (?x) (IgnoreWhiteSpace) allows you to make regexes more readable with insignificant whitespace: any unescaped whitespace can be used for visual formatting.


      • ^\\ matches the \ (escaped with ) at the start (^) of each string.


      • (.+?) matches one or more characters lazily.


      • .D matches a literal . followed by something other than a digit (d matches a digit, D is the negation of that).


      • .+ matches one or more remaining characters, i.e., the rest of the input.


      • $1 as the replacement operand refers to what the 1st capture group ((...)) in the regex matched, and, given that the regex was designed to consume the entire string, replaces it with just that.







      share|improve this answer















      Your regex succeeds in splitting off the tokens of interest in principle, but it doesn't account for the leading \ in the input strings.



      You can use regex alternation (|) to include the leading \ at the start as an additional -split separator.



      Given that matching a separator at the very start of the input creates an empty element with index 0, you then need to access index 1 to get the substring of interest.



      In short: The regex passed to -split should be '^\\|.(?!d)' instead of '.(?!d)', and the index used to access the resulting array should be [1] instead of [0]:



      '\tom.overflow.corp.com', '\123.43.234.23.overflow.corp.com' | 
      ForEach-Object { ($_ -Split '^\\|.(?!d)')[1] }


      The above yields:



      tom
      123.43.234.23




      Alternatively, you could remove the leading \ in a separate step, using -replace:



      '\tom.overflow.corp.com', '\123.43.234.23.overflow.corp.com' | 
      ForEach-Object { ($_ -Split '.(?!d)')[0] -replace '^\\' }




      Yet another alternative is to use a single -replace operation, which does not require a ForEach-Object call (doesn't require explicit iteration):



      '\tom.overflow.corp.com', '\123.43.234.23.overflow.corp.com' -replace
      '?(x) ^\\ (.+?) .D .+', '$1'



      • Inline option (?x) (IgnoreWhiteSpace) allows you to make regexes more readable with insignificant whitespace: any unescaped whitespace can be used for visual formatting.


      • ^\\ matches the \ (escaped with ) at the start (^) of each string.


      • (.+?) matches one or more characters lazily.


      • .D matches a literal . followed by something other than a digit (d matches a digit, D is the negation of that).


      • .+ matches one or more remaining characters, i.e., the rest of the input.


      • $1 as the replacement operand refers to what the 1st capture group ((...)) in the regex matched, and, given that the regex was designed to consume the entire string, replaces it with just that.








      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 28 '18 at 18:21

























      answered Nov 27 '18 at 23:28









      mklement0mklement0

      135k21252289




      135k21252289













      • Glad to hear it, @jimwood; my pleasure. As an aside: While I was personally notified of your comment due to being the author of this answer, the others who answered here were not.

        – mklement0
        Nov 30 '18 at 23:52



















      • Glad to hear it, @jimwood; my pleasure. As an aside: While I was personally notified of your comment due to being the author of this answer, the others who answered here were not.

        – mklement0
        Nov 30 '18 at 23:52

















      Glad to hear it, @jimwood; my pleasure. As an aside: While I was personally notified of your comment due to being the author of this answer, the others who answered here were not.

      – mklement0
      Nov 30 '18 at 23:52





      Glad to hear it, @jimwood; my pleasure. As an aside: While I was personally notified of your comment due to being the author of this answer, the others who answered here were not.

      – mklement0
      Nov 30 '18 at 23:52













      2














      I'm stealing Lee_Daileys $InSTuff
      but appending a RegEx I used recently



      $InStuff = -split @'
      \tom.overflow.corp.com
      \123.43.234.23.overflow.corp.com
      '@
      $InStuff |ForEach-Object {($_.Trim('\') -split '.(?!d{1,3}(.|$))')[0]}


      Sample Output:



      tom
      123.43.234.23


      As you can see here on RegEx101 the dots between the numbers are not matched






      share|improve this answer






























        2














        I'm stealing Lee_Daileys $InSTuff
        but appending a RegEx I used recently



        $InStuff = -split @'
        \tom.overflow.corp.com
        \123.43.234.23.overflow.corp.com
        '@
        $InStuff |ForEach-Object {($_.Trim('\') -split '.(?!d{1,3}(.|$))')[0]}


        Sample Output:



        tom
        123.43.234.23


        As you can see here on RegEx101 the dots between the numbers are not matched






        share|improve this answer




























          2












          2








          2







          I'm stealing Lee_Daileys $InSTuff
          but appending a RegEx I used recently



          $InStuff = -split @'
          \tom.overflow.corp.com
          \123.43.234.23.overflow.corp.com
          '@
          $InStuff |ForEach-Object {($_.Trim('\') -split '.(?!d{1,3}(.|$))')[0]}


          Sample Output:



          tom
          123.43.234.23


          As you can see here on RegEx101 the dots between the numbers are not matched






          share|improve this answer















          I'm stealing Lee_Daileys $InSTuff
          but appending a RegEx I used recently



          $InStuff = -split @'
          \tom.overflow.corp.com
          \123.43.234.23.overflow.corp.com
          '@
          $InStuff |ForEach-Object {($_.Trim('\') -split '.(?!d{1,3}(.|$))')[0]}


          Sample Output:



          tom
          123.43.234.23


          As you can see here on RegEx101 the dots between the numbers are not matched







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 27 '18 at 22:31

























          answered Nov 27 '18 at 22:24









          LotPingsLotPings

          19.5k61533




          19.5k61533























              1














              The Select-String function uses regex and populates a MatchInfo object with the matches (which can then be queried).



              The regex "(.?d+)+|w+" works for your particular example.



              "\tom.overflow.corp.com", "\123.43.234.23.overflow.corp.com" |
              Select-String "(.?d+)+|w+" | % { $_.Matches.Value }





              share|improve this answer






























                1














                The Select-String function uses regex and populates a MatchInfo object with the matches (which can then be queried).



                The regex "(.?d+)+|w+" works for your particular example.



                "\tom.overflow.corp.com", "\123.43.234.23.overflow.corp.com" |
                Select-String "(.?d+)+|w+" | % { $_.Matches.Value }





                share|improve this answer




























                  1












                  1








                  1







                  The Select-String function uses regex and populates a MatchInfo object with the matches (which can then be queried).



                  The regex "(.?d+)+|w+" works for your particular example.



                  "\tom.overflow.corp.com", "\123.43.234.23.overflow.corp.com" |
                  Select-String "(.?d+)+|w+" | % { $_.Matches.Value }





                  share|improve this answer















                  The Select-String function uses regex and populates a MatchInfo object with the matches (which can then be queried).



                  The regex "(.?d+)+|w+" works for your particular example.



                  "\tom.overflow.corp.com", "\123.43.234.23.overflow.corp.com" |
                  Select-String "(.?d+)+|w+" | % { $_.Matches.Value }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 28 '18 at 14:40









                  mklement0

                  135k21252289




                  135k21252289










                  answered Nov 28 '18 at 0:22









                  Claire FurneyClaire Furney

                  477617




                  477617























                      0














                      while this is NOT regex, it does work. [grin] i suspect that if you have a really large number of such items, then you will want a regex. they do tend to be faster than simple text operators.



                      this will get rid of the leading \ and then replace the domain name with .



                      # fake reading in a text file
                      # in real life, use Get-Content
                      $InStuff = -split @'
                      \tom.overflow.corp.com
                      \123.43.234.23.overflow.corp.com
                      '@

                      $DomainName = '.overflow.corp.com'

                      $InStuff.ForEach({
                      $_.TrimStart('\').Replace($DomainName, '')
                      })


                      output ...



                      tom
                      123.43.234.23





                      share|improve this answer
























                      • thank you. Not really what I am after, I don't want to add more code. Is '.(?!d)' not regex? I have got it to do almost what I want with that little bit of code. I will keep trying

                        – jim wood
                        Nov 27 '18 at 22:12











                      • @jimwood - it doesn't seem like "adding more code" from what i can see. [grin] the code posted by LotPings is significantly longer than what i posted. plus, my code is easily understood - regex is not quite so obvious. ///// however, if you need SPEED, then the regex method is likely the way to go.

                        – Lee_Dailey
                        Nov 27 '18 at 22:45











                      • My code will remove any trailing domain parts, while yours is fixed to a given one, so functionality counts not length. Nitpicking: mine is 75 chars/yours is ~100

                        – LotPings
                        Nov 27 '18 at 23:34






                      • 1





                        @LotPings - that was not intended as any criticism of your code. i'm sorry if it seemed so, tho. [blush] it was a criticism of the OPs rationale for rejecting mine. [grin] as i mentioned, regex is almost certainly going to be faster for a large data set - if the OP has such a need. ///// i presumed the OP gave only one domain for a reason other than "oops! i should have mentioned that will change". ///// as an aside, will your regex handle Tom.Dooley.Overflow.Corp.com? that is why i gave up on a regex ... i could not think of any way to handle the possible pre-domain parts. [blush]

                        – Lee_Dailey
                        Nov 28 '18 at 1:21


















                      0














                      while this is NOT regex, it does work. [grin] i suspect that if you have a really large number of such items, then you will want a regex. they do tend to be faster than simple text operators.



                      this will get rid of the leading \ and then replace the domain name with .



                      # fake reading in a text file
                      # in real life, use Get-Content
                      $InStuff = -split @'
                      \tom.overflow.corp.com
                      \123.43.234.23.overflow.corp.com
                      '@

                      $DomainName = '.overflow.corp.com'

                      $InStuff.ForEach({
                      $_.TrimStart('\').Replace($DomainName, '')
                      })


                      output ...



                      tom
                      123.43.234.23





                      share|improve this answer
























                      • thank you. Not really what I am after, I don't want to add more code. Is '.(?!d)' not regex? I have got it to do almost what I want with that little bit of code. I will keep trying

                        – jim wood
                        Nov 27 '18 at 22:12











                      • @jimwood - it doesn't seem like "adding more code" from what i can see. [grin] the code posted by LotPings is significantly longer than what i posted. plus, my code is easily understood - regex is not quite so obvious. ///// however, if you need SPEED, then the regex method is likely the way to go.

                        – Lee_Dailey
                        Nov 27 '18 at 22:45











                      • My code will remove any trailing domain parts, while yours is fixed to a given one, so functionality counts not length. Nitpicking: mine is 75 chars/yours is ~100

                        – LotPings
                        Nov 27 '18 at 23:34






                      • 1





                        @LotPings - that was not intended as any criticism of your code. i'm sorry if it seemed so, tho. [blush] it was a criticism of the OPs rationale for rejecting mine. [grin] as i mentioned, regex is almost certainly going to be faster for a large data set - if the OP has such a need. ///// i presumed the OP gave only one domain for a reason other than "oops! i should have mentioned that will change". ///// as an aside, will your regex handle Tom.Dooley.Overflow.Corp.com? that is why i gave up on a regex ... i could not think of any way to handle the possible pre-domain parts. [blush]

                        – Lee_Dailey
                        Nov 28 '18 at 1:21
















                      0












                      0








                      0







                      while this is NOT regex, it does work. [grin] i suspect that if you have a really large number of such items, then you will want a regex. they do tend to be faster than simple text operators.



                      this will get rid of the leading \ and then replace the domain name with .



                      # fake reading in a text file
                      # in real life, use Get-Content
                      $InStuff = -split @'
                      \tom.overflow.corp.com
                      \123.43.234.23.overflow.corp.com
                      '@

                      $DomainName = '.overflow.corp.com'

                      $InStuff.ForEach({
                      $_.TrimStart('\').Replace($DomainName, '')
                      })


                      output ...



                      tom
                      123.43.234.23





                      share|improve this answer













                      while this is NOT regex, it does work. [grin] i suspect that if you have a really large number of such items, then you will want a regex. they do tend to be faster than simple text operators.



                      this will get rid of the leading \ and then replace the domain name with .



                      # fake reading in a text file
                      # in real life, use Get-Content
                      $InStuff = -split @'
                      \tom.overflow.corp.com
                      \123.43.234.23.overflow.corp.com
                      '@

                      $DomainName = '.overflow.corp.com'

                      $InStuff.ForEach({
                      $_.TrimStart('\').Replace($DomainName, '')
                      })


                      output ...



                      tom
                      123.43.234.23






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 27 '18 at 21:28









                      Lee_DaileyLee_Dailey

                      2,4311811




                      2,4311811













                      • thank you. Not really what I am after, I don't want to add more code. Is '.(?!d)' not regex? I have got it to do almost what I want with that little bit of code. I will keep trying

                        – jim wood
                        Nov 27 '18 at 22:12











                      • @jimwood - it doesn't seem like "adding more code" from what i can see. [grin] the code posted by LotPings is significantly longer than what i posted. plus, my code is easily understood - regex is not quite so obvious. ///// however, if you need SPEED, then the regex method is likely the way to go.

                        – Lee_Dailey
                        Nov 27 '18 at 22:45











                      • My code will remove any trailing domain parts, while yours is fixed to a given one, so functionality counts not length. Nitpicking: mine is 75 chars/yours is ~100

                        – LotPings
                        Nov 27 '18 at 23:34






                      • 1





                        @LotPings - that was not intended as any criticism of your code. i'm sorry if it seemed so, tho. [blush] it was a criticism of the OPs rationale for rejecting mine. [grin] as i mentioned, regex is almost certainly going to be faster for a large data set - if the OP has such a need. ///// i presumed the OP gave only one domain for a reason other than "oops! i should have mentioned that will change". ///// as an aside, will your regex handle Tom.Dooley.Overflow.Corp.com? that is why i gave up on a regex ... i could not think of any way to handle the possible pre-domain parts. [blush]

                        – Lee_Dailey
                        Nov 28 '18 at 1:21





















                      • thank you. Not really what I am after, I don't want to add more code. Is '.(?!d)' not regex? I have got it to do almost what I want with that little bit of code. I will keep trying

                        – jim wood
                        Nov 27 '18 at 22:12











                      • @jimwood - it doesn't seem like "adding more code" from what i can see. [grin] the code posted by LotPings is significantly longer than what i posted. plus, my code is easily understood - regex is not quite so obvious. ///// however, if you need SPEED, then the regex method is likely the way to go.

                        – Lee_Dailey
                        Nov 27 '18 at 22:45











                      • My code will remove any trailing domain parts, while yours is fixed to a given one, so functionality counts not length. Nitpicking: mine is 75 chars/yours is ~100

                        – LotPings
                        Nov 27 '18 at 23:34






                      • 1





                        @LotPings - that was not intended as any criticism of your code. i'm sorry if it seemed so, tho. [blush] it was a criticism of the OPs rationale for rejecting mine. [grin] as i mentioned, regex is almost certainly going to be faster for a large data set - if the OP has such a need. ///// i presumed the OP gave only one domain for a reason other than "oops! i should have mentioned that will change". ///// as an aside, will your regex handle Tom.Dooley.Overflow.Corp.com? that is why i gave up on a regex ... i could not think of any way to handle the possible pre-domain parts. [blush]

                        – Lee_Dailey
                        Nov 28 '18 at 1:21



















                      thank you. Not really what I am after, I don't want to add more code. Is '.(?!d)' not regex? I have got it to do almost what I want with that little bit of code. I will keep trying

                      – jim wood
                      Nov 27 '18 at 22:12





                      thank you. Not really what I am after, I don't want to add more code. Is '.(?!d)' not regex? I have got it to do almost what I want with that little bit of code. I will keep trying

                      – jim wood
                      Nov 27 '18 at 22:12













                      @jimwood - it doesn't seem like "adding more code" from what i can see. [grin] the code posted by LotPings is significantly longer than what i posted. plus, my code is easily understood - regex is not quite so obvious. ///// however, if you need SPEED, then the regex method is likely the way to go.

                      – Lee_Dailey
                      Nov 27 '18 at 22:45





                      @jimwood - it doesn't seem like "adding more code" from what i can see. [grin] the code posted by LotPings is significantly longer than what i posted. plus, my code is easily understood - regex is not quite so obvious. ///// however, if you need SPEED, then the regex method is likely the way to go.

                      – Lee_Dailey
                      Nov 27 '18 at 22:45













                      My code will remove any trailing domain parts, while yours is fixed to a given one, so functionality counts not length. Nitpicking: mine is 75 chars/yours is ~100

                      – LotPings
                      Nov 27 '18 at 23:34





                      My code will remove any trailing domain parts, while yours is fixed to a given one, so functionality counts not length. Nitpicking: mine is 75 chars/yours is ~100

                      – LotPings
                      Nov 27 '18 at 23:34




                      1




                      1





                      @LotPings - that was not intended as any criticism of your code. i'm sorry if it seemed so, tho. [blush] it was a criticism of the OPs rationale for rejecting mine. [grin] as i mentioned, regex is almost certainly going to be faster for a large data set - if the OP has such a need. ///// i presumed the OP gave only one domain for a reason other than "oops! i should have mentioned that will change". ///// as an aside, will your regex handle Tom.Dooley.Overflow.Corp.com? that is why i gave up on a regex ... i could not think of any way to handle the possible pre-domain parts. [blush]

                      – Lee_Dailey
                      Nov 28 '18 at 1:21







                      @LotPings - that was not intended as any criticism of your code. i'm sorry if it seemed so, tho. [blush] it was a criticism of the OPs rationale for rejecting mine. [grin] as i mentioned, regex is almost certainly going to be faster for a large data set - if the OP has such a need. ///// i presumed the OP gave only one domain for a reason other than "oops! i should have mentioned that will change". ///// as an aside, will your regex handle Tom.Dooley.Overflow.Corp.com? that is why i gave up on a regex ... i could not think of any way to handle the possible pre-domain parts. [blush]

                      – Lee_Dailey
                      Nov 28 '18 at 1:21




















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53508163%2fregular-expressions-in-powershell-split%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                      Calculate evaluation metrics using cross_val_predict sklearn

                      Insert data from modal to MySQL (multiple modal on website)