powershell -ilike operations too similar - powershell

is there a way to say, that between a * there is only a numeric value of maybe two values.
i want to select items, but the way i can differentiate them is very limited.
i want to store values like "31.04.2003" with following line of code:
$contentDateReal = $content_ -ilike '*"*.*.*",'
this works for me, in the most times but, sometimes i got values like: "Installation Acrobat Reader 10.0.1 "
those one also fit the -ilike filter but i dont want them. is there a way to say, that i only want values that contains numbers, and that before the first dot, there is only 2 ("xx") index sizes, after the first dot also ("xx"), and after the second one there is space for four index values like "xxxx" or "2020".

While, you can use character ranges such as [0-9] to match a character (digit) in that range, PowerShell's wildcard expressions do not support matching a varying number of these characters.
That is, '10' -like '[0-9][0-9]' is $true, but '2' -like '[0-9][0-9]' is not.
Note: -ilike is just an alias for -like, which is case-insensitive by default, as all PowerShell operators are; conversely, use -clike for case-sensitive matching. This naming convention applies to all operators that (also) process text.
While you do want to match fixed numbers of digits, matching with a fixed number of [0-9] ranges may still yield false positives if additional digits are present at the start or at the end, so to rule these out you need to use the more sophisticated matching that regular expressions (regexes) provide:
PowerShell supports regexes via the -match operator (among others), so you could use the following:
('Some Software 31.04.2003', 'Installation Acrobat Reader 10.0.1').ForEach({
if ($_ -match '\b(\d{2}\.\d{2}\.\d{4})\b') {
"'$_' matched; extracted version number: $($Matches[1])"
}
})
The above yields the following, because only the first string matched:
'Some Software 31.04.2003' matched; extracted version number: 31.04.2003
Explanation of the regex:
\b matches at word boundaries, which means that something other than word character (a letter, a digit, or _) must occur at that position (which can include the start and end of the string).
\d matches a digit (roughly equivalent to [0-9], the latter limiting matching to the decimal digits in the ASCII sub-range of Unicode); {2}, for instance, stipulates that exactly 2 instances of digits must be present.
\. represents a verbatim . (it must be \-escaped, because . is a regex metacharacter representing any character).
Enclosing a subexpression in (...) creates a so-called capture group, which additionally captures what the subexpression matched, and makes that available starting with index 1 (for the first of potentially multiple (unnamed) capture groups) in the automatic $Matches variable variable.
Note that -match - unlike -like - matches substrings by default, so there's no need to also match what comes before or after the version number.

Related

Extract anchor tag link text using Powershell

I'm attempting to extract the link text from something like the line below using PowerShell.
Entertainment, Intimate Apparel/Swimsuit, and Suspicious
I've tried the following but it's only matching the first result and is including the > and < which I don't want. I'm sure it's an issue with the Regex but I don't know it well enough to see what's wrong. Note the string above is $result.categorization
$result.categorization -match '(\>(.*?)\<)'
This returns
Name,Value
2,Entertainment
1,>Entertainment<
0,>Entertainment<
I want to return
Name,Value
2,Suspicious
1,Intimate Apparel/Swimsuit
0,Entertainment
I also tried the Regex listed Regular expression to extract link text from anchor tag but that didn't match on anything.
I don't know where the headers and numbers in the output come from, but here's a solution that extracts the link texts from the single-line input exactly as specified:
$str = #'
Entertainment, Intimate Apparel/Swimsuit, and Suspicious
'#
$str -split ', and |, ' -replace '.*?>([^<]*).*', '$1'
$str -split ', and |, ' splits the input line into individual <a> elements.
-replace then operates on each <a> element individually:
'.*?>([^<]*).*' matches the entire line, but captures only the link text in the one and only capture group, (...).
Replacement text $1 then replaces the entire line with what the capture group matched, i.e., effectively only returning the link text.
As for what you tried:
-match never extracts part of its input - it returns a Boolean indicating whether a match was found with a scalar LHS, or a filtered sub-array of matching items with an array as the LHS.
That said, the automatic $Matches variable does contain information about what parts matched, but only with a scalar LHS.
'(\>(.*?)\<)' contains two nested capture groups that match literal > followed by any number of characters (matching non-greedily), followed by literal <.
It is the inner capture group that would capture the link text.
However:
There is no need for the outer capture group.
> and < do not need \-escaping in a regular expression (although it does no harm).

Text file search for match strings regex

I am trying to understand how regex works and what are the possibilities of working with it.
So I have a txt file and I am trying to search for 8 char long strings containing numbers. for now I use a quite simple option:
clear
Get-ChildItem random.txt | Select-String -Pattern [0-9][a-z] | foreach {$_.line}
It sort of works but I am trying to find a better option. ATM it takes too long to read through the left out text since it writes entire lines and it does not filter them by length.
You can use a lookahead to assert that a string contains at least 1 digit, then specify the length of the match and finally anchor it with ^ (start of string) and $ (end of string) if the string is on a line of its own, or \b (word boundary) if it's part of an HTML document as your comments seem to suggest:
Get-ChildItem C:\files\ |Select-String -Pattern '^(?=.*\d)\w{8}$'
Get-ChildItem C:\files\ |Select-String -Pattern '\b(?=.*\d)\w{8}\b'
The pattern [0-9][a-z] matches a digit followed by a letter. If you want to match a sequence of 8 characters use .{8}. The dot in regular expressions matches any character except newlines. A number in curly brackets matches the preceding expression the given number of times.
If you want to match non-whitespace characters use \S instead of .. If you want to match only digits and letters use [0-9a-z] (a character class) instead of ..
For a more thorough introduction please go find a tutorial. The subject is way too complex to be covered by a single answer on SO.
What you're currently searching for is a single number ranging from 0-9 followed by a single lowercase letter ranging from a-z.
this, for example, will match any 8 char long strings containing only alphanumeric characters.
\w{8}
i often forget what some regex classes are, and it may be useful to you as a learning tool, but i use this as a point of reference: http://regexr.com/
It can also validate what you're typing inline via a text field so you can see if what you're doing works or not.
If you need more of a tutorial than a reference, i found this extremely useful when i learned: regexone.com

Need to use regular expressions inside of a function to match a string passed to it

What I'd like to do is create a function that is passed a string to match a regular expression inside of the function. Let's call the function "matching." It should use the -match command. I want it to meet the criteria:
The < character
Four alphabetic characters of upper of lowercase (a-z or A-Z)
The > character
The - character
Four digits, 0-9
So basically it would just look like "matching whateverstringisenteredhere" then it'd give me true or false. Probably incredibly simple for you guys, but to someone new at powershell it seems really difficult.
So far I have this:
function matching ($args0)
{
$r = '\b[A-Za-z]{4}[0-9]{4}<>-\b'
$r -match ($args0)
}
The problem seems to be it's not treating it as a regular expression inside the function. It's only taking it literally.
The regex goes on the right side of the -match operator and the string to be matched goes on the left. Try:
$args0 -match $r

Alternating string

I am trying to use regular expressions to match against a string that starts with 7 numbers, then has a "K" inbetween it, and then 3 numbers again. For example:
1234567K890.
I currently have $_a -match '^\d{7}K\d{3}'. However, this does not work for my purposes. Does anyone have a solution?
PS C:\> "1234567K890" -match "\d{7}(k)\d{3}"
This \d{7} matches 7 digits then (k) matches letter k and \d{3} matches last three characters.
Tested this, works for your example and some others:
$string = "1234567K890"
$string -match '^[0-9]{7}(k)[0-9]{3}$'"
It matches against exactly 7 numbers, then against K (casing does not matter), then against exactly 3 numbers. The characters at the beginning and the end of the string restrict against whitespace at the beginning and end of the string -- if you want whitespace to be allowed, you can just remove them.
Here's a powershell regex reference, which may help in the future.

How to get a perfect match for a regexp pattern in Perl?

I've to match a regular-expression, stored in a variable:
#!/bin/env perl
use warnings;
use strict;
my $expr = qr/\s*(\w+(\[\d+\])?)\s+(\w+(\[\d+\])?)/sx;
$str = "abcd[3] xyzg[4:0]";
if ($str =~ m/$expr/) {
print "\n%%%%%%%%% $`-----$&-----$'\n";
}
else {
print "\n********* NOT MATCHED\n";
}
But I'm getting the outout in $& as
%%%%%%%%% -----abcd[3] xyzg-----[4:0]
But expecting, it shouldn't go inside the if clause.
What is intended is:
if $str = "abcd xyzg" => %%%%%%%%% -----abcd xyzg----- (CORRECT)
if $str = "abcd[2] xyzg" => %%%%%%%%% -----abcd[2] xyzg----- (CORRECT)
if $str = "abcd[2] xyzg[3] => %%%%%%%%% -----abcd[2] xyzg[3]----- (CORRECT)
if $str = "abcd[2:0] xyzg[3] => ********* NOT MATCHED (CORRECT)
if $str = "abcd[2:0] xyzg[3:0] => ********* NOT MATCHED (CORRECT)
if $str = "abcd[2] xyzg[3:0]" => ********* NOT MATCHED (CORRECT/INTENDED)
but output is %%%%%%%%% -----abcd[2] xyzg-----[3:0] (WRONG)
OR better to say this is not intended.
In this case, it should/my_expectation go to the else block.
Even I don't know, why $& take a portion of the string (abcd[2] xyzg), and $' having [3:0]?
HOW?
It should match the full, not a part like the above. If it didn't, it shouldn't go to the if clause.
Can anyone please help me to change my $expr pattern, so that I can have what is intended?
By default, Perl regexes only look for a matching substring of the given string. In order to force comparison against the entire string, you need to indicate that the regex begins at the beginning of the string and ends at the end by using ^ and $:
my $expr = qr/^\s*(\w+(\[\d+\])?)\s+(\w+(\[\d+\])?)$/;
(Also, there's no reason to have the /x modifier, as your regex doesn't include any literal whitespace or # characters, and there's no reason for the /s modifier, as you're not using ..)
EDIT: If you don't want the regex to match against the entire string, but you want it to reject anything in which the matching portion is followed by something like "[0:0]", the simplest way would be to use lookahead:
my $expr = qr/^\s*(\w+(\[\d+\])?)\s+(\w+(\[\d+\]|(?=[^[\w])|$ ))/x;
This will match anything that takes the following form:
beginning of the string (which your example in the comments seems to imply you want)
zero or more whitespace characters
one or more word characters
optional: [, one or more digits, ]
one or more whitespace characters
one or more word characters
one of the following, in descending order of preference:
[, one or more digits, ]
an empty string followed by (but not including!) a character that is neither [ nor a word character (The exclusion of word characters is to keep the regex engine from succeeding on "a[0] bc[1:2]" by only matching "a[0] b".)
end of string (A space is needed after the $ to keep it from merging with the following ) to form the name of a special variable, and this entails the reintroduction of the /x option.)
Do you have any more unstated requirements that need to be satisfied?
The short answer is your regexp is wrong.
We can't fix it for you without you explaining what you need exactly, and the community is not going to write a regexp exactly for your purpose because that's just too localized a question that only helps you this one time.
You need to ask something more general about regexps that we can explain to you, that will help you fix your regexp, and help others fix theirs.
Here's my general answer when you're having trouble testing your regexp. Use a regexp tool, like the regex buddy one.
So I'm going to give a specific answer about what you're overlooking here:
Let's make this example smaller:
Your pattern is a(bc+d)?. It will match: abcd abccd etc. While it will not match bcd nor bzd in the case of abzd it will match as matching only a because the whole group of bc+d is optional. Similarly it will match abcbcd as a dropping the whole optional group that couldn't be matched (at the second b).
Regexps will match as much of the string as they can and return a true match when they can match something and have satisfied the entire pattern. If you make something optional, they will leave it out when they have to including it only when it's present and matches.
Here's what you tried:
qr/\s*(\w+(\[\d+\])?)\s+(\w+(\[\d+\])?)/sx
First, s and x aren't needed modifiers here.
Second, this regex can match:
Any or no whitespace followed by
a word of at least one alpha character followed by
optionally a grouped square bracketed number with at least one digit (eg [0] or [9999]) followed by
at least one white space followed by
a word of at least one alpha character followed by
optionally a square bracketed number with at least one digit.
Clearly when you ask it to match abcd[0] xyzg[0:4] the colon ends the \d+ pattern but doesn't satisfy the \] so it backtracks the whole group, and then happily finds the group was optional. So by not matching the last optional group, your pattern has matched successfully.