Invalid Expression Pattern - regex

The following Regex pattern in PowerShell is giving me real trouble. The double and single quotes are the culprits but I don't know how to get PowerShell to accept it. How do I get PowerShell to successfully accept this pattern?
If I copy the pattern to a variable PowerShell complains about an unexpected token after the first quote found within the pattern.
$myRegex = "^param[\s?]*\([\$\sa-zA-Z\=\#\(\)\"\d\,\:\\\_\.\']*\)"
I then attempted to escape the double quote by adding another quote next to it. This time the string is accepted but the regex fails. Notice the double double quote in the next example.
$myRegex = "^param[\s?]*\([\$\sa-zA-Z\=\#\(\)\""\d\,\:\\\_\.\']*\)"
$somelongString -replace $myRegex
Error Message:
The regular expression pattern ^param[\s?]*\([\$\sa-zA-Z\=\#\(\)\"\d\,\:\\\_\.\']*\) is not valid.
Update 1:
Per #Dan Farrell's suggestion I updated my regex as follows:
$myRegex = "^param(\s?)*\([\$\sa-zA-Z\=\#\(\)\""\d\,\:\\\_\.\']*\)"
Update 2:
This is a working example of my Regex which I am trying to port to PowerShell

Escaping _ in a .NET regex causes an error. To use a " inside "..." string literal, you need to escape it with a backtick, use `". Besides, you only need to escape \ inside your character class.
Use
$myRegex = "^param\s*\([$\sa-zA-Z=#()`"\d,:\\_.']*\)"

Related

Regex Expression - text between quotes and brackets

i have the following JSON string that i need to parse:
{'ConnectionDetails':'{\'server\':\'johnssasd02\',\'database\':\'enterprise analytics\'}'}]}
i am already using the expression '([^']*)' to get everything in quotes, which correctly gets me the ConnectionDetails title. However i now need an expression to get me everything between '{ and '} in order to get the full path value. so i need to capture the following from above string:
{\'server\':\'johnssasd02\',\'database\':\'enterprise analytics\'}
but having trouble coming up the regex expression
thanks
In order to extract the data between the curly braces {} you can use the regex: \{(.*?)\}
i accomplished it within an SSIS derived column task where i removed unwanted characters from the input string. that way i don't have to worry about dealing with them using regex.

Escape string in PowerShell Regex to a regular string

I have a string that contains some regex in, $(=?, Now this string is a password that I need to pass for some application that I'm building.
The code that I'm trying to use is:
$x = 'GIWs#K?hks2v&HKXb$S9=HK*AZN=i!(S?7'
[Regex]::Escape($x)
I've already tried the method with [Regex]::Escape() and it doesn't meet my requirements because I'm trying to insert the string as a password and it replacing the Regex with \.
Perhaps after I'm doing the [Regex]::Escape() should I try to delete the \ that I'm getting from the result of the command?
After running the [Regex]::Escape() this is the result I'm getting when printing the output:
GIWs#K\?hks2v&HKXb=HK\*AZN=i!\(S\?7
I'm trying to achieve the string without the ' \ ' characters but with the Escape function:
GIWs#K?hks2v&HKXb=HK*AZN=i!(S?7
This is not an answer because I don't know what the problem actually is. However, there are some inherent problems with your current attempt to handle the password string. If you use double quotes ("") around a string, PowerShell will interpolate the string inside the quotes. So any alphanumeric characters following an unescaped $, will be considered a variable name during interpolation. If that variable has no value, $variable will be replaced with a null value. You can see this behavior below:
"rt4837s$GT=\"
rt4837s=\
You should use single quotes ('') when quoting string literals (characters that will be left as is). PowerShell will not attempt interpolation when unescaped single quote pairs are encountered unless there is quote nesting. See below:
'rt4837s$GT=\'
rt4837s$GT=\
If you need a regex escaped string, the same rules apply from above and you should use single quotes.
[regex]::escape('dfaseryh$S9=r??*')
dfaseryh\$S9=r\?\?\*
If for any reason, you need to access that string later without the escape characters, then you can use the regex method Unescape().
[regex]::unescape('dfaseryh\$S9=r\?\?\*')
dfaseryh$S9=r??*
Practical Example of Using Regex Replace:
$OriginalString = 'Username = Anonymous; Password = <password>'
$regexReplace = [regex]::Escape('<password>')
$Password = 'GIWs#K?hks2v&HKXb$S9=HK*AZN=i!(S?7'
$OriginalString -replace $regexReplace,($Password -replace '\$','$$$$')
# Output
Username = Anonymous; Password = GIWs#K?hks2v&HKXb$S9=HK*AZN=i!(S?7
In the code above, $OriginalString is just an ordinary string that can be retrieved from any command or set by a coder. It contains a string <password> that we want to replace with a complex password string GIWs#K?hks2v&HKXb$S9=HK*AZN=i!(S?7.
$Password contains the complex password. Since we only care about replacing <password> and are choosing to use regex replace operator -replace, we need a valid regex expression for matching <password>. There is a caveat here though. When using -replace, the $ in the replacement string is used to prefix capture group names. So there can be cases where the literal string has an unintentional replacement. Capture group 0 is always there if there is a match. So $0 will always cause issues without proper escaping. It is probably best to just escape $ regardless.
For the regex match, we use [regex]::Escape('<password>') since we are unsure if <> are special in regex. If there are no special characters, then the string within the regex expression will not be modified. If it does contain special characters, they will be escaped with \.
As a result, <password> is replaced with GIWs#K?hks2v&HKXb$S9=HK*AZN=i!(S?7.
A recap of the syntax is as follows:
'String With Something You Want to Replace' -replace 'Regex Expression to Match String You Want to Replace','Replacement That Is a Literal String With Escaped $'

regex_error. What's wrong with my regex

I'm getting regex_error for some reason. I also tried it the regular way of using escape characters (this method eliminates the need for escape sequences in c++ 11 by putting R"(something)" )
By the way if anyone was wondering, they are for recognizing lines in xml
When I use a web based regex tester it works fine.
string sstart = R"(\w*+(> ? +[^\\])++>)";
string send = R"(.*<\\\w\w[^m-o][^_]++)";
string sdata = R"([^>]++>[^ ]++)";
regex endtag(send);
regex taganddata(sdata);
regex starttag(sstart);
Syntax of you regular expressions is incorrect because of '++' part.
.+ matches one or more occurrences. But what do you try to match with .++ ?

Asterisk regular expression : Invalid preceding regular expression

I am trying to verify if inbound CLI matchest one of these patterns:
CLI STARTING WITH:
+39
0039
3
0[1-9]
So i wrote the following
exten => s,n,Set(isita=${REGEX("^(+39|0039|3|0[1-9])" ${cli})})
However I am getting this error :
Malformed input REGEX(): Invalid preceding regular expression
What is wrong with my regular expression?
You need to escape the +, use this RegEx instead:
^(\\+39|0039|3|0[1-9])
You can see the error when you Test it on RegExr
Normally in a RegEx (in JavaScript for example, whre is it enclosed in /), you only need one \, however when the RegEx is stored in a string (in this case anyway), you need 2 \.
If you have one \, the string is trying to create a character based on \+ (like \n is a newline). You need the second \ to state that the first \ should not be converted.
New RegEx on RegExr
Answer is correct, but use of REGEXP inside dialplan is not so nice idea. Dialplan itself is regexp, it have form for do regexp based on cli
exten => _s/_39.,n,Noop(do something for cli starting with 39)
So it more asterisk-way use dialplan, not regexp.

I want to modify this regex to include apostrophe

This regex is used for validating email addresses, however it doesn't include the case for apostrophy (') which is a valid character in the first part of an email address.
I have tried myself and to use some examples I found, but they don't work.
^([\w-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$
How do I modify it slightly to support the ' character (apostraphy)?
Per the documentation for an email address, the apostrophe can appear anywhere before the # symbol, which, in your current regex is:
^([\w-\.]+)#
You should be able to add the apostrophe into the brackets of valid characters:
^([\w-\.']+)#
This would make the entire regex:
^([\w-\.']+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$
EDIT (regex contained in single-quotes)
If you're using this regex inside a string with single-quotes, such as in PHP with $regex = '^([\w ..., you will need to escape the single-quote in the regex with \':
^([\w-\.\']+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$
You need to update the first part as follows:
^([\'\w-\.]+)