RegEx in dry practicing will not run

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
monoeagle
Posts: 108
Last visit: Fri Jan 26, 2024 10:44 am

RegEx in dry practicing will not run

Post by monoeagle »

Hi@All,

I have a little Problem with my dry practice.

source

Code: Select all

 test (aaa-b-cc11)
 test (aaa-b-cc11)
 test (aaa-b-cc12)
 www-b-cc13
 zzz-b-cc14
 test (aaa-b-cc11)
 www-b-cc13
result should be

Code: Select all

 aaa-b-cc11
 aaa-b-cc12
 www-b-cc13
 zzz-b-cc14

Code: Select all

$result = $doc.SelectNodes("//Object") |  Where-Object {($_.Name -like '*-b-cc1*')}  | %{$_.Name} | %{ $_ -Replace '^.*([a-z]{3}-b-cc1[1-4])$','$1' } | Sort-Object -Unique
$result
The problem is that the resultlist looks like the source, sorted and unique but the regex won't be applicated.

with the following replace the resultlist is ok

Code: Select all

$result = $doc.SelectNodes("//NCPObject") |  Where-Object {($_.Name -like '*-b-cc1*')}  |  % { $_.Name } | % { ([String]$_).Replace("Test","")} | % { ([String]$_).Replace(" (","")} | % { ([String]$_).Replace(")","")} | Sort-Object -Unique
but its static and if something will change out of the names I have to change the row in the script.

The second static way:

Code: Select all

$result = $doc.SelectNodes("//NCPObject") |  Where-Object {($_.Name -like '*-b-cc1*')}  | Select-Object -ExpandProperty Name | %{
if($_.Length -gt 10){
    ($_).Substring(5,10)
}
} | Sort-Object -Unique
The funny thing at this try is that the resultlist ist smaller than the first static way. cc12 and cc14 will be lost in the result list but the like in the where-object doesn't change.

But if the RegEx will work I can ignore the static ways.
Where is the error in reasoning?

regards
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: RegEx in dry practicing will not run

Post by jvierra »

What is a "dry practice"? What is it that you are trying to do?

It appears that you may be trying to extract something from some unknown XML. What you have provided is unclear and what you are asking is too vague. I know it seems clear to you but we cannot see what you are doing. Please try to ask a clear and complete question.
User avatar
monoeagle
Posts: 108
Last visit: Fri Jan 26, 2024 10:44 am

Re: RegEx in dry practicing will not run

Post by monoeagle »

sry
=)

dry practice(from the translator or in german "trockenübung") --> cutting the script down and testing the smallest part in the ISE.

The source list is the result from an xml file, which I can't upload that's why I postet what as source is delivered.

Code: Select all

$doc.SelectNodes("//Object") |  Where-Object {($_.Name -like '*-b-cc1*')}  | %{$_.Name} | %{ $_ -Replace '^.*([a-z]{3}-b-cc1[1-4])$','$1' } | Sort-Object -Unique
$result
Step 1: select all nodes where Name strukture is *-b-cc1* --> runs
Step 2: just give me the Name Values --> runs
Step 3: correct the Values that the list just contains the correct part --> didn't run with RegEx, in the static ways it runs

As in the first Post, the resultlist should be without "test", "(" and ")" in the Value.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: RegEx in dry practicing will not run

Post by jvierra »

Are you trying to say that you want to extract what is inside of "()". What is it you are trying to do. You are saying a lot but failing to ask a clear question.

'(test)' -match '\((.*)\)'
$matches[1]
User avatar
monoeagle
Posts: 108
Last visit: Fri Jan 26, 2024 10:44 am

Re: RegEx in dry practicing will not run

Post by monoeagle »

hmmm sry I thought that what the source and result should be is understandable.

Yes I want the name inside the "(" <name> ")".

That's why I wrote the RegEx '^.*([a-z]{3}-b-cc1[1-4])$' to find in a variable string the Nameblock.

the first 3 positions are letters
-b-cc1 is allready the same
the last number could be 1 till 4

If I try your example at my snippet:

Code: Select all

$result= $doc.SelectNodes("//NCPObject") |  Where-Object {($_.Name -like '*-b-cc1*')}  |  % { $_.Name } | %{
if($_.Length -gt 10){
 $_ -match '\((.*)\)'
 $matches[1]
}
} | Sort-Object -Unique
But it doesn't work.
User avatar
monoeagle
Posts: 108
Last visit: Fri Jan 26, 2024 10:44 am

Re: RegEx in dry practicing will not run

Post by monoeagle »

new dummy try

Code: Select all

$result = ''

$input = @('aaa-b-cc11';'aaa-b-cc12';'aaa-b-cc14';'vvv-b-cc11';'test (rrr-b-cc11)')
$input

Write-Host '-------------------'

$result = @()

foreach($value in $input){

#$value.Length

if($value.Length -gt 10){

     if($value -match '\((.*)\)'){
     
     $new = $result += $matches[1]
     }
}else{
     $new = $result += $value.ToString()
}
}

$new
Output

Code: Select all

aaa-b-cc11
aaa-b-cc12
aaa-b-cc14
vvv-b-cc11
test (rrr-b-cc11)
-------------------
aaa-b-cc11
aaa-b-cc12
aaa-b-cc14
vvv-b-cc11
rrr-b-cc11
=) It runs. =) THX
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: RegEx in dry practicing will not run

Post by jvierra »

"$input" is a reserved value and cannot be assigned. Much of your syntax is incorrect or illegal. I recommend that you go back to the books and review the basics. Check the syntax of each line you try to use. Check the full help for the syntax or command. Eventually you will learn where you are missing basic information on how to use PowerShell.

The following works exactly as expected. Find all of my changes and try to understand why I made the change. It will help you find some of our misunderstanding of PowerShell.

Code: Select all

$values = @(
	'aaa-b-cc11',
	'aaa-b-cc12',
	'aaa-b-cc14',
	'vvv-b-cc11',
	'test (rrr-b-cc11)'
)

foreach ($value in $values) {
	if ($value -match '\((.*)\)') {
		Write-Host ('Match:'+$matches[1]) -fore green
	} else {
		Write-Host ('No Match:' + $value) -ForegroundColor red
	}
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: RegEx in dry practicing will not run

Post by jvierra »

The short version is this:

$results = $values | ForEach-Object{if($_ -match '\((.*)\)') {$matches[1]}else{$_}}
User avatar
monoeagle
Posts: 108
Last visit: Fri Jan 26, 2024 10:44 am

Re: RegEx in dry practicing will not run

Post by monoeagle »

Good morning jvierra,

thx

if you mean the ";"'s in the $input, why it doesn't throw an syntaxerror?

regards
This topic is 6 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked