How to trim a word from a sentence using powershell

I’m having a set of lines like below:

Public class ABC extended version of line

Public class MNO extended version of line

I’ve 100s of lines like this. I need only the word which is after class.

In the above example its ABC and MNO.

Can we use the trim concept here ? Or any other ideas are much appreciated.

You can use -match to get these values :slight_smile:

# with -match
$FileContents |
    Where-Object { $_ -match 'class (?<ClassName>\w+)' } |
    ForEach-Object { $matches['ClassName'] }

Another one using switch statement.

switch -regex -file ($FileName) {
    'class (\w+)' {$matches[1]}

Thanks for your response :slight_smile:

My output looks like this :

public class ABCD extends AbstractBshDefaultTaskPrep {

public class EFGH extends somename {

Here i want to have the output as public class ABCD and not as public class ABCD extends AbstractBshDefaultTaskPrep {

This is code i am trying with is this … Where can i use the switch or the trim concept or any other concept in the below code to get the desired output.

function Find_ClassAndItsMethods { 
$List = get-childitem "C:\Programs\data\Git\tc-pdm\src\Customization\Source" -recurse -filter "*.java"
$Results = foreach ($file in $list) {
$Text = Get-Content -Path $file.FullName
$Classes = $Test | Where-Object {$_ -like '* class*'} 
$Methods = $Text -match '\w+\([^\)]*\)' -ireplace '.*?(?<Method>(\w+ \w+|\w+\.\w+)\([^\)]*\)).*','${Method}'
File_Name = $file.FullName
Class_Name = $Classes -join '; '
Methods = $Methods -join '; '
$Results | Export-Csv -Path C:\Programs\export.csv -NoTypeInformation

Did you try the examples provided above in previous replies ? is the output shown above by using those methods ?

Yes , I did try with your example and its working absolutely fine … Thank you for that …

But i also need for abstract class and final class :

The below snippet is displaying only for the “class” . I need for “abstract class” and “final class” as well . How can we modify the below snippet for this requirement .

switch -regex -file ($FileName) {
'class (\w+)' {$matches[1]}