-2

In Atom, if I use Ctrl-F with regular expressions, it will highlight all the matches of the regular expression, for example

File:

aaa
b
ccc
d
e
f
gg

Query:

[a-z]{2,}

Highlighted text:

aaa

ccc



gg

But I haven't found a cmdlet to do this in PowerShell

Expected input/output:

$var = @"
aaa
b
ccc
d
e
f
gg
"@

$var -find "[a-z]{2,}"

#Output
aaa
ccc
gg

Is there a way I can do this?

6
  • Have you tried anything? Commented Jun 30, 2020 at 5:26
  • 2
    @NekoMusume No. Commented Jun 30, 2020 at 5:26
  • Remember to use google... Does stackoverflow.com/questions/4726074/… answer your question? Commented Jun 30, 2020 at 5:28
  • I tried $regex = [regex]'[a-z]{2,}' and $regex.match($var) and got incorrect result Commented Jun 30, 2020 at 5:30
  • .match only gets the first result, .matches gets multiple. Is that what you want? Commented Jun 30, 2020 at 5:33

2 Answers 2

3

What you are looking for is

$regex = [regex]'[a-z]{2,}'
$regex.Matches($var).value

Explanation:

As in the comments I suggested you look at Powershell using Regex finding a string within a string where the answer demonstrated the [regex]'s datatype .match method. Unfortunately, this only grabs the first result of a regex and is not meant for multiple results, however, matches will get multiple as it is the plural form of match:

.match:

# Define a regex query
$regex = [regex]'[a-z]{2,}'

# Define your variable
$var = @"
aaa
b
ccc
d
e
f
gg
"@

# Query $var
$regex.match($var)
#output


Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 0
Length   : 3
Value    : aaa


As you can see this only gets aaa and provides other data like what group it was in, if it succeeded, and other information.

However, if we do the same thing but with the matches method, it will get all 3 results:

$regex.matches($var)
#output


Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 0
Length   : 3
Value    : aaa

Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 6
Length   : 3
Value    : ccc

Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 16
Length   : 2
Value    : gg



Which shows information for all three results. But if we want to narrow down the results to just the values, we can grab the value tag of the variable like

$regex.Matches($var).value
#output
aaa
ccc
gg

Which is (I assume from your question) what you want.

Sign up to request clarification or add additional context in comments.

2 Comments

If I want value and length, what do I do?
Like @(@{Length = $regex.Matches($var).length}; @{Value = $regex.Matches($var).value})?
2

You can use Select-String to get your desired output:

$var = @"
aaa
b
ccc
d
e
f
gg
"@
$var -split '\r\n' | Select-String -Pattern "[a-z]{2,}"

Output:

aaa
ccc
gg

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.