Get an array of captures from a regex search in PowerShell
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
add a comment |
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
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
add a comment |
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
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
regex powershell
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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.
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
add a comment |
A bit shorten version:
@(Select-String "[a-zA-Z]+(?=d)" -Input "14 be h90 bfh4" -AllMatches).Matches.Value
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
A bit shorten version:
@(Select-String "[a-zA-Z]+(?=d)" -Input "14 be h90 bfh4" -AllMatches).Matches.Value
add a comment |
A bit shorten version:
@(Select-String "[a-zA-Z]+(?=d)" -Input "14 be h90 bfh4" -AllMatches).Matches.Value
add a comment |
A bit shorten version:
@(Select-String "[a-zA-Z]+(?=d)" -Input "14 be h90 bfh4" -AllMatches).Matches.Value
A bit shorten version:
@(Select-String "[a-zA-Z]+(?=d)" -Input "14 be h90 bfh4" -AllMatches).Matches.Value
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
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 26 '18 at 6:38
Lieven KeersmaekersLieven Keersmaekers
47.8k1190125
47.8k1190125
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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