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

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

Related

powershell -ilike operations too similar

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.

Can I use split on a string two times in a row using piping?

Let's say I have the following string dog.puppy/cat.kitten/bear.cub. I want to get bear only. I can do this by executing the following:
$str = "dog.puppy/cat.kitten/bear.cub"
$str = $str.split('/')[2]
$str = $str.split('.')[0]
Am I able to get bear using one line using piping? This doesn't work, but it would be something like this:
$str = "dog.puppy/cat.kitten/bear.cub"
$str = $str.split('/')[2] | $_.split('.')[0]
Am I able to get "bear" using one line using piping?
With strings already stored in memory, there is no good reason to use the pipeline - it will only slow things down.
Instead, use PowerShell's operators, which are faster than using a pipeline and generally more flexible than the similarly named .NET [string] type's methods, because they operate on regexes (regular expressions) and can also operate on entire arrays:
PS> ('dog.puppy/cat.kitten/bear.cub' -split '[/.]')[-2]
bear
That is, split the input string by either literal / or literal . (using a character set, [...]), and return the penultimate (second to last) ([-2]) token.
See this answer for why you should generally prefer the -split operator to the String.Split() method, for instance.
The same applies analogously to preferring the -replace operator to the String.Replace() method.
It's easy to chain these operators:
PS> ('dog.puppy/cat.kitten/bearOneTwoThree.cub' -split '[/.]')[-2] -csplit '(?=\p{Lu})'
bear
One
Two
Three
That is, return the penultimate token and split it case-sensitively (using the -csplit variation of the -split operator) whenever an uppercase letter (\p{Lu}) starts, using a positive look-ahead assertion, (?=...).
You can do this:
$str = "dog.puppy/cat.kitten/bear.cub".split('/')[2].split('.')[0]
No piping needed.
There is an easier solution,
the .split() method uses each char to split,
using a negative index counts from back, so
> "dog.puppy/cat.kitten/bear.cub".split( '/.')[-2]
bear
Your other question can be solved with the RegEx based -csplit operator using a
nonconsuming positive lookahead and
a final -join ' ' to concatenate the array elements with a space.
$str = "dog.puppy/cat.kitten/bearOneTwoThree.cub"
$str = $str.split('/.')[-2] -csplit '(?=[A-Z])' -join ' '
$str
bear One Two Three

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

Strip all non letters and numbers from a string in Powershell?

I have a variable that has a string which recieves strange characters like hearts.
Besides that point, I wanted to know anyhow: How do I leave a string with only letters and numbers, discarding the rest or replacing it with nothing (not adding a space or anything)
My first thought was using a regular expression but I wanted to know if Powershell had something more native that does it automatically.
Let say you have variable like this:
$temp = '^gdf35#&fhd^^h%(#$!%sdgjhsvkushdv'
you can use the -replace method to replace only the Non-word characters like this:
$temp -replace "\W"
The result will be:
gdf35fhdhsdgjhsvkushdv
Consider white-listing approved characters, and replace anything that isn't whitelisted:
$temp = 'This-Is-Example!"£$%^&*.Number(1)'
$temp -replace "[^a-zA-Z0-9]"
ThisIsExampleNumber1
This gives added flexibility if you ever do want to include non-alphanumeric characters which may be expected as part of the string (e.g. dot, dash, space).
$temp = 'This-is-example!"£$%^&*.Number(2)'
$temp -replace "[^a-zA-Z0-9.-]"
This-is-example.Number2

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.