Get an array of captures from a regex search in PowerShell












1















Let's say I have this string:



"14 be h90 bfh4"


And I have this regex pattern:



"(w+)d"


In PowerShell, how do I get an array with the contents {"h", "bfh"}?










share|improve this question























  • i am not very good with regex but im trying to learn. Is this what you want? $array = ($string -split '(s)') -replace '[^a-zA-Z-]','' | SELECT-STRING -Pattern '([A-Z])'

    – Owain Esau
    Nov 26 '18 at 5:31











  • Try $result = Select-String 'b(p{L}+)d+b' -Input "14 be h90 bfh4" -AllMatches|%{$_.Matches.Groups[1].Value}

    – Wiktor Stribiżew
    Nov 26 '18 at 9:45


















1















Let's say I have this string:



"14 be h90 bfh4"


And I have this regex pattern:



"(w+)d"


In PowerShell, how do I get an array with the contents {"h", "bfh"}?










share|improve this question























  • i am not very good with regex but im trying to learn. Is this what you want? $array = ($string -split '(s)') -replace '[^a-zA-Z-]','' | SELECT-STRING -Pattern '([A-Z])'

    – Owain Esau
    Nov 26 '18 at 5:31











  • Try $result = Select-String 'b(p{L}+)d+b' -Input "14 be h90 bfh4" -AllMatches|%{$_.Matches.Groups[1].Value}

    – Wiktor Stribiżew
    Nov 26 '18 at 9:45
















1












1








1








Let's say I have this string:



"14 be h90 bfh4"


And I have this regex pattern:



"(w+)d"


In PowerShell, how do I get an array with the contents {"h", "bfh"}?










share|improve this question














Let's say I have this string:



"14 be h90 bfh4"


And I have this regex pattern:



"(w+)d"


In PowerShell, how do I get an array with the contents {"h", "bfh"}?







regex powershell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 5:05









NetherGraniteNetherGranite

702325




702325













  • i am not very good with regex but im trying to learn. Is this what you want? $array = ($string -split '(s)') -replace '[^a-zA-Z-]','' | SELECT-STRING -Pattern '([A-Z])'

    – Owain Esau
    Nov 26 '18 at 5:31











  • Try $result = Select-String 'b(p{L}+)d+b' -Input "14 be h90 bfh4" -AllMatches|%{$_.Matches.Groups[1].Value}

    – Wiktor Stribiżew
    Nov 26 '18 at 9:45





















  • i am not very good with regex but im trying to learn. Is this what you want? $array = ($string -split '(s)') -replace '[^a-zA-Z-]','' | SELECT-STRING -Pattern '([A-Z])'

    – Owain Esau
    Nov 26 '18 at 5:31











  • Try $result = Select-String 'b(p{L}+)d+b' -Input "14 be h90 bfh4" -AllMatches|%{$_.Matches.Groups[1].Value}

    – Wiktor Stribiżew
    Nov 26 '18 at 9:45



















i am not very good with regex but im trying to learn. Is this what you want? $array = ($string -split '(s)') -replace '[^a-zA-Z-]','' | SELECT-STRING -Pattern '([A-Z])'

– Owain Esau
Nov 26 '18 at 5:31





i am not very good with regex but im trying to learn. Is this what you want? $array = ($string -split '(s)') -replace '[^a-zA-Z-]','' | SELECT-STRING -Pattern '([A-Z])'

– Owain Esau
Nov 26 '18 at 5:31













Try $result = Select-String 'b(p{L}+)d+b' -Input "14 be h90 bfh4" -AllMatches|%{$_.Matches.Groups[1].Value}

– Wiktor Stribiżew
Nov 26 '18 at 9:45







Try $result = Select-String 'b(p{L}+)d+b' -Input "14 be h90 bfh4" -AllMatches|%{$_.Matches.Groups[1].Value}

– Wiktor Stribiżew
Nov 26 '18 at 9:45














3 Answers
3






active

oldest

votes


















2














You want to capture one or more alphabets that are followed by a number, hence the regex for what you want to capture would be this,



[a-zA-Z]+(?=d)


And the powershell code for same will be this,



$str = "14 be h90 bfh4"
$reg = "[a-zA-Z]+(?=d)"
$spuntext = $str | Select-String $reg -AllMatches |
ForEach-Object { $_.Matches.Value }
echo $spuntext


Disclaimer: I barely know powershell scripting language so you may have to tweak some codes.






share|improve this answer
























  • The powershell part does need rework but the gist of it looks good

    – Lieven Keersmaekers
    Nov 26 '18 at 6:37











  • Thanks @LievenKeersmaekers. I actually just googled about powershell and came up with this. I've never got a chance to use powershell before. But its good, everything has a first time :)

    – Pushpesh Kumar Rajwanshi
    Nov 26 '18 at 6:43



















1














A bit shorten version:



@(Select-String "[a-zA-Z]+(?=d)" -Input "14 be h90 bfh4" -AllMatches).Matches.Value





share|improve this answer

































    1














    Multiple ways to skin a cat as demonstrated by the other answers. Yet another way would be by using the [regex] object provided by .Net



    $regex = [regex] '([a-z]+)(?=d+)'
    $regex.Matches("14 be h90 bfh4") | Select Value





    share|improve this answer























      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%2f53475036%2fget-an-array-of-captures-from-a-regex-search-in-powershell%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      You want to capture one or more alphabets that are followed by a number, hence the regex for what you want to capture would be this,



      [a-zA-Z]+(?=d)


      And the powershell code for same will be this,



      $str = "14 be h90 bfh4"
      $reg = "[a-zA-Z]+(?=d)"
      $spuntext = $str | Select-String $reg -AllMatches |
      ForEach-Object { $_.Matches.Value }
      echo $spuntext


      Disclaimer: I barely know powershell scripting language so you may have to tweak some codes.






      share|improve this answer
























      • The powershell part does need rework but the gist of it looks good

        – Lieven Keersmaekers
        Nov 26 '18 at 6:37











      • Thanks @LievenKeersmaekers. I actually just googled about powershell and came up with this. I've never got a chance to use powershell before. But its good, everything has a first time :)

        – Pushpesh Kumar Rajwanshi
        Nov 26 '18 at 6:43
















      2














      You want to capture one or more alphabets that are followed by a number, hence the regex for what you want to capture would be this,



      [a-zA-Z]+(?=d)


      And the powershell code for same will be this,



      $str = "14 be h90 bfh4"
      $reg = "[a-zA-Z]+(?=d)"
      $spuntext = $str | Select-String $reg -AllMatches |
      ForEach-Object { $_.Matches.Value }
      echo $spuntext


      Disclaimer: I barely know powershell scripting language so you may have to tweak some codes.






      share|improve this answer
























      • The powershell part does need rework but the gist of it looks good

        – Lieven Keersmaekers
        Nov 26 '18 at 6:37











      • Thanks @LievenKeersmaekers. I actually just googled about powershell and came up with this. I've never got a chance to use powershell before. But its good, everything has a first time :)

        – Pushpesh Kumar Rajwanshi
        Nov 26 '18 at 6:43














      2












      2








      2







      You want to capture one or more alphabets that are followed by a number, hence the regex for what you want to capture would be this,



      [a-zA-Z]+(?=d)


      And the powershell code for same will be this,



      $str = "14 be h90 bfh4"
      $reg = "[a-zA-Z]+(?=d)"
      $spuntext = $str | Select-String $reg -AllMatches |
      ForEach-Object { $_.Matches.Value }
      echo $spuntext


      Disclaimer: I barely know powershell scripting language so you may have to tweak some codes.






      share|improve this answer













      You want to capture one or more alphabets that are followed by a number, hence the regex for what you want to capture would be this,



      [a-zA-Z]+(?=d)


      And the powershell code for same will be this,



      $str = "14 be h90 bfh4"
      $reg = "[a-zA-Z]+(?=d)"
      $spuntext = $str | Select-String $reg -AllMatches |
      ForEach-Object { $_.Matches.Value }
      echo $spuntext


      Disclaimer: I barely know powershell scripting language so you may have to tweak some codes.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 26 '18 at 6:29









      Pushpesh Kumar RajwanshiPushpesh Kumar Rajwanshi

      10.7k21229




      10.7k21229













      • The powershell part does need rework but the gist of it looks good

        – Lieven Keersmaekers
        Nov 26 '18 at 6:37











      • Thanks @LievenKeersmaekers. I actually just googled about powershell and came up with this. I've never got a chance to use powershell before. But its good, everything has a first time :)

        – Pushpesh Kumar Rajwanshi
        Nov 26 '18 at 6:43



















      • The powershell part does need rework but the gist of it looks good

        – Lieven Keersmaekers
        Nov 26 '18 at 6:37











      • Thanks @LievenKeersmaekers. I actually just googled about powershell and came up with this. I've never got a chance to use powershell before. But its good, everything has a first time :)

        – Pushpesh Kumar Rajwanshi
        Nov 26 '18 at 6:43

















      The powershell part does need rework but the gist of it looks good

      – Lieven Keersmaekers
      Nov 26 '18 at 6:37





      The powershell part does need rework but the gist of it looks good

      – Lieven Keersmaekers
      Nov 26 '18 at 6:37













      Thanks @LievenKeersmaekers. I actually just googled about powershell and came up with this. I've never got a chance to use powershell before. But its good, everything has a first time :)

      – Pushpesh Kumar Rajwanshi
      Nov 26 '18 at 6:43





      Thanks @LievenKeersmaekers. I actually just googled about powershell and came up with this. I've never got a chance to use powershell before. But its good, everything has a first time :)

      – Pushpesh Kumar Rajwanshi
      Nov 26 '18 at 6:43













      1














      A bit shorten version:



      @(Select-String "[a-zA-Z]+(?=d)" -Input "14 be h90 bfh4" -AllMatches).Matches.Value





      share|improve this answer






























        1














        A bit shorten version:



        @(Select-String "[a-zA-Z]+(?=d)" -Input "14 be h90 bfh4" -AllMatches).Matches.Value





        share|improve this answer




























          1












          1








          1







          A bit shorten version:



          @(Select-String "[a-zA-Z]+(?=d)" -Input "14 be h90 bfh4" -AllMatches).Matches.Value





          share|improve this answer















          A bit shorten version:



          @(Select-String "[a-zA-Z]+(?=d)" -Input "14 be h90 bfh4" -AllMatches).Matches.Value






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 6:36









          Lieven Keersmaekers

          47.8k1190125




          47.8k1190125










          answered Nov 26 '18 at 6:35









          Kirill PashkovKirill Pashkov

          2,4951816




          2,4951816























              1














              Multiple ways to skin a cat as demonstrated by the other answers. Yet another way would be by using the [regex] object provided by .Net



              $regex = [regex] '([a-z]+)(?=d+)'
              $regex.Matches("14 be h90 bfh4") | Select Value





              share|improve this answer




























                1














                Multiple ways to skin a cat as demonstrated by the other answers. Yet another way would be by using the [regex] object provided by .Net



                $regex = [regex] '([a-z]+)(?=d+)'
                $regex.Matches("14 be h90 bfh4") | Select Value





                share|improve this answer


























                  1












                  1








                  1







                  Multiple ways to skin a cat as demonstrated by the other answers. Yet another way would be by using the [regex] object provided by .Net



                  $regex = [regex] '([a-z]+)(?=d+)'
                  $regex.Matches("14 be h90 bfh4") | Select Value





                  share|improve this answer













                  Multiple ways to skin a cat as demonstrated by the other answers. Yet another way would be by using the [regex] object provided by .Net



                  $regex = [regex] '([a-z]+)(?=d+)'
                  $regex.Matches("14 be h90 bfh4") | Select Value






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 6:38









                  Lieven KeersmaekersLieven Keersmaekers

                  47.8k1190125




                  47.8k1190125






























                      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%2f53475036%2fget-an-array-of-captures-from-a-regex-search-in-powershell%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

                      To store a contact into the json file from server.js file using a class in NodeJS

                      Redirect URL with Chrome Remote Debugging Android Devices

                      Dieringhausen