need help with regex

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 1 year and 2 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
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

need help with regex

Post by Domtar »

hi,

I have a list of languages detected from SCCM and I would like to get only the language name.

My SCCM query returns this array:

Autopilot - English Devices
Autopilot - French Devices

I would like to get only English & French from that array. i can easily use a regex to extract one language from one line with

.*Autopilot - (\w*) Devices.*

but i cannot do it with 2 elements like shown above

can someone please help me understanding how to get all the results i need? this will be useful because we are planning more languages in the future.

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

Re: need help with regex

Post by jvierra »

Just detect the string language you want.
if ($line -match 'English'){
#do something
}else($line -match 'French')
}


Use one line for each language you need to detect. You can do this in a loop with an array of lines.
User avatar
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

Re: need help with regex

Post by Domtar »

good morning,

i cannot check if english or french, like i said i'd like to use a regex because we will add languages in the future and i do not wish to re-visit this code to add spanish or german or whateveer is added.

not even sure how many languages there will be at the end.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: need help with regex

Post by jvierra »

I see no way to solve your issue without changing code when you want to detect new languages. Try to rethink your process and question. Perhaps you will be able to clarify what you are trying to do.
This topic is 1 year and 2 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