This question already has answers here:
Why are all newlines gone after PowerShell's Get-Content, Regex, and Set-Content?
(4 answers)
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 2 years ago.
The goal is simple:
Take contents of text file
Search for pattern
Save search back to the file
For example, i want to find the first occurrence between # and ##. Following regex works perfectly (\#)(.*?)(?=\#{2}). It finds what I want. However, PowerShell removes all new line characters effectively changing the formatting. So, following input text
#
This
Is
My
Text
##
becomes this
# This Is My Text
How to preserve the formatting?
Here is my PowerShell script
param (
[string]$filename
)
$content = Get-Content -Path $filename
$output = $filename
$regex = [Regex]::new('(\#)(.*?)(?=\#{2})')
$matches = $regex.Matches($content)
Set-Content -Path $output $Matches[0]
This question already has answers here:
Why do Perl string operations on Unicode characters add garbage to the string?
(7 answers)
Closed 3 years ago.
I want to check if a person's name is valid.
It should check latin letters, also with umlauts (i.e. öäüÖÄÜé).
unfortunately nothing i've tried works.
regarding many sources (following some links),
https://www.regular-expressions.info/unicode.html
Regex for word characters in any language
\p{L} should work, but it doesn't works for me.
Do i have to use a library for this?
use strict;
use warnings;
my $test = "testString";
print $1 if ($test =~ m/^(\p{L}+)$/); #testString
$test = "testStringö";
print $1 if ($test =~ m/^(\p{L}+)$/); #no print msg
$test = "testéString";
print $1 if ($test =~ m/^(\p{L}+)$/); #no print msg
You need to tell Perl that the source code of your file is in utf8. Add
use utf8;
After
use strict;
This question already has answers here:
How can I extract the matches from the Perl match operator into variables?
(6 answers)
Closed 5 years ago.
The following perl code only gives back true or false (1 & 0)
#!/usr/bin/perl
use strict;
use warnings;
my $string;
$string ="interface Ethernet1/20
shutdown";
my $test = $string =~ m/^.+$(?=\s+shutdown)/mg;
print "'$test'\n";
I get back a 1.
But how can I get back the matched string 'interface Ethernet1/20' ?
Thanks for every help!
Simply give it list context:
my ($test) = $string =~ m/^.+$(?=\s+shutdown)/mg;
The concept of evaluation context (list vs scalar) is fundamental to Perl programming, so it may be time to review some tutorials and/or a reference manual.
This question already has answers here:
Lambda Expression in Powershell
(3 answers)
Closed 3 years ago.
I am writing a powershell script to parse the HTM file. I need to find all the links file in the file and then uppercase the filepath, filename and extention. (could be 30 or 40 links in any file). The part I'm having trouble with is the 2nd part of the -replace staement below (the 'XXXX' part). The regex WILL find the strings I'm looking for but I can't figure out how to 'replace' that string with a uppercase version, then update the existing file with a new links.
I hope I'm explaining this correctly. Appreciate any assistance that anyone can provide.
This is the code I have so far...
$FilePath = 'C:\WebDev'
$FileName = 'Class.htm'
[regex]$regex='(href="[^.]+\.htm")'
#Will Match the string href="filepath/file.htm"
( Get-Content "$FilePath\$FileName") -replace $regex , 'XXXX' | Set-Content "$FilePath\$FileName";
Final string that gets updated in the existing file should look like this HREF="FILEPATH/FILE.HTM"
Both beatcracker and briantist refer you to this answer, which shows the correct approach. Regex expressions cannot convert to uppercase, so you need to hook into the .NET String.ToUpper() function.
Instead of using -replace, use the .Replace() method on your $regex object (as described in the other answer). You also need the ForEach-Object construct so it gets called for each string in the pipeline. I've split up the last line for readability, but you can keep it on one line if you must.
$FilePath = 'C:\WebDev'
$FileName = 'Class.htm'
[regex]$regex='(href="[^.]+\.htm")'
(Get-Content "$FilePath\$FileName") |
ForEach-Object { $regex.Replace($_, {$args[0].Value.ToUpper()}) } |
Set-Content "$FilePath\$FileName"
This question already has answers here:
How can I parse quoted CSV in Perl with a regex?
(7 answers)
Closed 7 years ago.
I have a string that looks something like this:
'som,ething', another, 'thin#'g', 'her,e', gggh*
I am trying to get it to split on the commas that are NOT in the elements, like this:
'som,ething'
another
'thin#'g'
'her,e'
gggh*
I am using parse_line(q{,}, 1, $string) but it seems to fail when the string has single quotes in it. Is there something I'm missing?
#!/usr/bin/perl
use strict;
use warnings;
my $string = q{'som,ething', another, 'thin'g', 'her,e', gggh*};
my #splitted = split(/,(?=\s+)/, $string);
print $_."\n" foreach #splitted;
Output:
'som,ething'
another
'thin'g'
'her,e'
gggh*
Demo
It looks like you're trying to parse comma-separated values. The answer is to use Text::CSV_XS since that handles the various weird cases you're likely to find in the data. See How can I parse quoted CSV in Perl with a regex?
Using split is not the way to go. If you are sure your string is well formatted using a global match is more simple, example:
my $line = "'som,ething', another , 'thin#'g', 'her,e' , gggh*";
my #list = $line =~ /\s*('[^#']*(?:#.[^#']*)*+'|[^,]+(?<=\S))\s*/g;
print join("|", #list);
(the (?<=\S) is only here to trim items on the right)