Asterisk regular expression : Invalid preceding regular expression - regex

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.

Related

Invalid Expression Pattern

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,:\\_.']*\)"

Search and convert to lower case on vim

I have a code with object.attribute where attribute can be an array
example: object.SIZE_OF_IMAGE[0] or a simple string. I want to search all occurrences "object.attribute" and replace it with self.lowercase(attribute) I want a regular expression on vim to do that.
I can use that :%s/object.*/self./gc and replace it manually but it is very slow.
Here are some examples:
object.SIZE to self.size
object.SIZE_OF_IMAGE[0] to self.size_of_image[0]
You basically just need two things:
Capture groups :help /\( let you store what's matched in between \(...\) and then reference it (via \1, \2, etc.) in the replacement (or even afterwards in the pattern itself).
The :help s/\L special replacement action that makes everything following lowercase.
This gives you the following command:
:%substitute/\<object\.\(\w\+\)/self.\L\1/g
Notes:
I've established a keyword start assertion (\<) at the beginning to avoid matching schlobject as well.
\w\+ matches letters, digits, and underscores (so it fulfills your example); various alternatives are possible here.
sed -E 's/object\.([^ \(]*)(.*)/self.lowercase(\1)\2/g' file_name.txt
above command considers that your attribute is followed by space or "("
you can tweek this command based on your need
Based on your comment above that the attribute part
"finishes by space or [ or (" you could match it with:
/object\.[^ [(]*
So, to replace it with self.attribute use a capturing
group and \L to make everything lowercase:
:%s/\vobject\.([^ [(]*)/self.\L\1/g
In the command mode try this
:1,$ s/object.attribute/self.lowercase(attribute)/g

Validator.matchesRegex in Mule blows up with basic pattern

I am receiving a date in message as a string. the following regular expression will confirm it is in at least the format I know I can handle:
^[0-9]{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$
but when I provide this regex to the validator.matchesRegex method in the Mule Expression language like so:
<when expression="#[validator.matchesRegex(payload.DateOfBirth,'^[0-9]{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$') == false]">
<set-variable variableName="validation_message" value="{"error": "Invalid DateOfBirth"}" doc:name="invalid DateOfBirth"/>
</when>
I receive the following error:
org.mule.api.MessagingException: [Error: illegal escape sequence: -]
[Near : {... eOfBirth,'^[0-9]{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[1 ....}]
^
[Line: 1, Column: 55] (org.mule.api.expression.InvalidExpressionException). Message payload is of type: HashMap
update
I've tried two new alterations:
doubling up my curly brackets like so:
^[0-9]{{4}}-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$
but I get the same error but a few characters off:
[Error: illegal escape sequence: -]
[Near : {... fBirth,'^[0-9]{{4}}\-(0?[1-9]|1[012])\-(0?[1-9]|[1 ....}]
^
[Line: 1, Column: 57] (org.mule.api.expression.InvalidExpressionException). Message payload is of type: HashMap
unescaping the dashes between the numbers
'^[0-9]{{4}}-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$
but I get the following error:
org.mule.api.MessagingException: Execution of the expression "validator.matchesRegex(payload.DateOfBirth,'^[0-9]{{4}}-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$') == false" failed. (org.mule.api.expression.ExpressionRuntimeException). Message payload is of type: HashMap
I have changed the pattern to not use the escape backslash to only accept the dash:
^[0-9]{4}.(0?[1-9]|1[012]).(0?[1-9]|[12][0-9]|3[01])$
I have replaced '-' with '.' which is not really what I want, but it at least validates the number portions. I have confirmed this works. it just allows invalid values such as '2016!02_23' when it really should only allow '2016-02-23'
TL;DR: there is a bug with regular expressions within MEL when you are trying to escape the dash character?
Mule (as this page indicates) does not give a way to avoid the evil escaped escape. If you use a \ in your regex, you must escape it: \\. Java is the same way.
Also, you need to understand that certain regex symbols do not always need to be escaped. This is very important when you work in Mule/Java, because it means you avoid the evil escaped escape.
Depending on where they appear in the regex, characters may either gain or lose meaning as meta characters. The - character only has special meaning when it is sandwiched inside [] character classes. This means that you just can just use it normally instead of escaping it in your regular expression.
I suggest that you read up on regexes.
There will be times that you need to use the evil escaped escape, which can get confusing. Personally, I usually use this site to convert my regexes into escaped Strings.

Regex working in regex engine but not in postgresql

I tried to match number 13 in pipe separated string like the one below:
13 - match
1|2|13 - match
13|1|2 - match
1|13|2 - match
1345|1|2 - should fail
1|1345|2 - should fail
1|2|1345 - should fail
1|4513|2 - should fail
4513|1|2 - should fail
2|3|4|4513- should fail
So, if 13 only occurs at the beginning or end, or in-between the string as a whole word it should match.
For that I wrote the following regex:
^13$|(\|13\|)?(?(1)|(^13\||\|13$))
In Regex101 it is working as expected. Please click link to see my sample.
But in Postgresql it throws error for the following query:
SELECT * FROM tbl_privilage WHERE user_id = 24 and show_id ~ '^13$|(\|13\|)?(?(1)|(^13\||\|13$))';
Error:
ERROR: invalid regular expression: quantifier operand invalid
SQL state: 2201B
Don't use a regex, using an array is more robust (and maybe more efficient as well):
select *
from the_table
where '13' = any (string_to_array(the_column, '|'));
this assumes that there is no whitespace between the values and the delimiter. You can even index that expression which probably makes searching a lot faster.
But I agree with Frank: you should really fix your data model.
Documentation is quite clear, saying that operator ~ implements the POSIX regular expressions. In Regex101 you're using PCRE (Perl-compatible) regular expressions. The two are very different.
If you need PCRE regular expressions in PostgreSQL you can setup an extension. Like pgpcre.
You need to match 13 within word boundaries.
You need
[[:<:]]13[[:>:]]
This solution should work even if you have spaces around the numeric values.
See documentation:
There are two special cases of bracket expressions: the bracket
expressions [[:<:]] and [[:>:]] are constraints, matching empty
strings at the beginning and end of a word respectively.

URL Regular Expression in Racket

I'm trying to use the URL regular expression to match URLs in Racket like this:
(regexp-match #rx"((mailto\:|(news|(ht|f)tp(s?))\:\/\/){1}\S+)" "www.test.com/")
The problem is that I'm getting this error: read: unknown escape sequence \: in string. What should I do to correct this?
Now I'm trying this:
(regexp-match #px"((mailto:|(news|(ht|f)tp(s?))://){1}\S+)" "www.youtube.com/watch?v=I0r4Wo2Q3l4")
And now I'm getting this error: read: unknown escape sequence \S in string
There are a number of issues with your code. First, as others have pointed out, you don't need to escape the colon character.
Second, you need to use #px to start a regular expression that uses perl-regexp extensions, as you've done.
Finally, you've left out the "http://" in the input that makes it match the pattern.
Here's an example that works:
#lang racket
(regexp-match #px"((mailto:|(news|(ht|f)tp(s?))://){1}\\S+)"
"http://www.test.com/")
running this code produces:
'("http://www.test.com/" "http://www.test.com/" "http://" "http" "ht" "")
\: is an incorrect scape sequence because : isn't a special character did you wanted to write .?