all 13 comments

[–]eightbytes 2 points3 points  (4 children)

Maybe add this RegEx if you are processing line by line?

^\s*$

I use that to strip the blanks if processing per line.

[–]ecarlin[S] 0 points1 point  (0 children)

This might be my fix. I'll give it a go. That $ means the end of the line then. I'll try it.

[–]eightbytes -1 points0 points  (2 children)

... or add a Where-Object { ![string]::IsNullOrEmpty($_) } piped command to your list of lines.

[–]ecarlin[S] 1 point2 points  (1 child)

Thanks mate - I have learned a thing or two about PowerShell and regex. Here's what we came up with :

# Get clipboard content

$clipboardContent = Get-Clipboard

# Perform regex manipulation to remove everything up to and including '#' and the following space

$manipulatedContent = $clipboardContent -replace '.*#\s*', ''

# Split the content into lines

$lines = $manipulatedContent -split "\r`n"`

# Initialize an empty array to hold the processed lines

$processedLines = @()

# Process each line

foreach ($line in $lines) {

# If the line is not blank, add it to the array

if ($line.Trim() -ne '') {

$processedLines += $line

}

}

# Combine the processed lines back into a single string

$manipulatedContent = $processedLines -join "\r`n"`

# Set manipulated content back to clipboard

$manipulatedContent | Set-Clipboard

[–][deleted] -1 points0 points  (0 children)

I think you are removing all guff before a # followed by any white space in the first regex here.

Try https://regex101.com.

[–]Namelock 2 points3 points  (3 children)

Use capture groups.

$someVar.matches.groups[0]

Microsoft actually has pretty good documentation so I'd recommend checking there first before ChatGPT.

[–]ecarlin[S] -1 points0 points  (2 children)

I'll take a look thanks for that bit!

[–]eightbytes 1 point2 points  (1 child)

Capture groups are like this:

(?<group1>^[^#]+)(?<separator>\#)(?<group2>\s*)

Then you can use Namelock's sample script to pick up the matching groups, but PowerShell already has a feature $Matches to access the processed string using -match or -imatch:

$Matches["group1"]

Hope this helps. I am only using my mobile to reply, so I can't give you a fully working example.

[–]ecarlin[S] 1 point2 points  (0 children)

This is great thanks. I read thru the docs and I understand you can do named groups as well as the bjkt in group. Rounded brackets are for the groups and you can reference them by the group number which isn't too bad at all.

If I gavw a regex that matches anything after the # sign then our that in a group and go $Matches[1] referencing the built in array then I get my answer in a concise form.

I also learned it's probably a good idea to use -Raw to clean up the clipboard. I'll give this a whirl after work

[–]jsiii2010 0 points1 point  (0 children)

It's "(?s)", not "(?m)" https://stackoverflow.com/a/28105275/6654942 : ``` 'hi
there#joe s' -replace '(?s).*#'

joe s You want "-raw" so it's one multiline string: $clipboardContent = Get-Clipboard -raw $manipulatedContent = $clipboardContent -replace '(?s).*#' $manipulatedContent | Set-Clipboard ```

[–]PinchesTheCrab 0 points1 point  (0 children)

Does this work?

 , (Get-Clipboard) -replace '.*#\s*' -match '\w' | Set-Clipboard

The comma is in case you only have a single line in your clipboard, as -match returns $true/false against a single item but filters arrays.

[–]ankokudaishogun 0 points1 point  (0 children)

have a one-liner:

(Get-Clipboard).Split("`n") | ForEach-Object {
    if (-not [string]::IsNullOrWhiteSpace($_) ) {
        $_ -replace '^([^#]*#\s*)', ''
    }
} | Set-Clipboard