URL Regular Expression in Racket - regex

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 .?

Related

How do i select only the files that starts with either CH or OTC [duplicate]

I'm creating a javascript regex to match queries in a search engine string. I am having a problem with alternation. I have the following regex:
.*baidu.com.*[/?].*wd{1}=
I want to be able to match strings that have the string 'word' or 'qw' in addition to 'wd', but everything I try is unsuccessful. I thought I would be able to do something like the following:
.*baidu.com.*[/?].*[wd|word|qw]{1}=
but it does not seem to work.
replace [wd|word|qw] with (wd|word|qw) or (?:wd|word|qw).
[] denotes character sets, () denotes logical groupings.
Your expression:
.*baidu.com.*[/?].*[wd|word|qw]{1}=
does need a few changes, including [wd|word|qw] to (wd|word|qw) and getting rid of the redundant {1}, like so:
.*baidu.com.*[/?].*(wd|word|qw)=
But you also need to understand that the first part of your expression (.*baidu.com.*[/?].*) will match baidu.com hello what spelling/handle????????? or hbaidu-com/ or even something like lkas----jhdf lkja$##!3hdsfbaidugcomlaksjhdf.[($?lakshf, because the dot (.) matches any character except newlines... to match a literal dot, you have to escape it with a backslash (like \.)
There are several approaches you could take to match things in a URL, but we could help you more if you tell us what you are trying to do or accomplish - perhaps regex is not the best solution or (EDIT) only part of the best solution?

Laravel Routes Regular Expression start with # character

I need to create a route that responses to any string starting with '#' character. Routes like following examples :
www.mywebsite.com/#john
www.mywebsite.com/#jack
www.mywebsite.com/#something
So I wrote:
Route::get('{something}','SomeController#someMethod')->where('something','/#^/');
But when I test it, I face 404 not found found page.
what is the correct regular expression for this?
Route::get('/{tag}', 'SomeController#someMethod')->where('tag', '^#.*');
This will also work:
Route::get('#{something}', 'SomeController#someMethod');
You can write this
Route::pattern('tag', '#[a-zA-Z]');
Route::get('{tag}', 'SomeController#someMethod');
This way you seperate the logic of the regex and the route and it will work as you want
Note the #^ pattern means # should be followed with the beginning of string, which is not possible, and the pattern never matches any string. The '^#' pattern asserts the position at the start of the string, and only there does it try to match #.
Also, the usual / regex delimiters should be removed from this pattern as they are treated as part of the pattern here.
So, in your case you may just swap the anchor and the # char:
Route::get('{something}','SomeController#someMethod')->where('something','^#');

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.

Trouble converting regex

This regex:
"REGION\\((.*?)\\)(.*?)END_REGION\\((.*?)\\)"
currently finds this info:
REGION(Test) my user typed this
END_REGION(Test)
I need it to instead find this info:
#region REGION my user typed this
#endregion END_REGION
I have tried:
"#region\\ (.*?)\\\n(.*?)#endregion\\ (.*?)\\\n"
It tells me that the pattern assignment has failed. Can someone please explain what I am doing wrong? I am new to Regex.
It seems the issue lies in the multiline \n. My recommendation is to use the modifier s to avoid multiline complexities like:
/#region\ \(.*?\)(.*?)\s#endregion\s\(.*?\)/s
Online Demo
s modifier "single line" makes the . to match all characters, including line breaks.
Try this:
#region(.*)?\n(.*)?#endregion(.*)?
This works for me when testing here: http://regexpal.com/
When using your original text and regex, the only thing that threw it off is that I did not have a new line at the end because your sample text didn't have one.
Constructing this regex doesn't fail using boost, even if you use the expanded modifier.
Your string to the compiler:
"#region\\ (.*?)\\\n(.*?)#endregion\\ (.*?)\\\n"
After parsed by compiler:
#region\ (.*?)\\n(.*?)#endregion\ (.*?)\\n
It looks like you have one too many escapes on the newline.
if you present the regex as expanded to boost, an un-escaped pound sign # is interpreted as a comment.
In that case, you need to escape the pound sign.
\#region\ (.*?)\\n(.*?)\#endregion\ (.*?)\\n
If you don't use the expanded modifier, then you don't need to escape the space characters.
Taking that tack, you can remove the escape on the space's, and fixing up the newline escapes, it looks like this raw (what gets passed to regex engine):
#region (.*?)\n(.*?)#endregion (.*?)\n
And like this as a source code string:
"#region (.*?)\\n(.*?)#endregion (.*?)\\n"
Your regular expression has an extra backslash when escaping the newline sequence \\\n, use \\s* instead. Also for the last capturing group you can use a greedy quantifier instead and remove the newline sequence.
#region\\ (.*?)\\s*(.*?)#endregion\\ (.*)
Compiled Demo

Regex string for IRC

I am trying to write a regex string to match a string revived from an IRC channel.
The message will be in the format "!COMMAND parameters"; the only command that is built by the system so far is repeat.
The regex I am using looks like this:
/![repeat] (.*?)/
When other commands are added it will look like:
/![cmd1|cmd2|cmd3] (.*?)/
It does not seem to be matching the right things in the string. Can anyone offer any input on this?
It appears that I need to add some basic regex stuff.
() brackets return data, [] matches but does not return.
Swapping to () does not work either.
The IRC program I am writing has a dynamic number of commands, so far I have only added "repeat" so the command pattern is "[repeat]". If I added "say", it would be "[repeat|say]".
Use the parentheses for grouping:
/!(cmd1|cmd2|cmd3) (.*)/
The brackets […] denote a character class describing just one character out of a set of characters.
You should also not use a non-greedy .* as the minimal match of .*? is an empty string.
You used bad brackets
/!(cmd1|cmd2|cmd3) (.*)/
I don't understand what did you mean with ? in your regex
[repeat] is a character class and will match r or e or p etc..., you should just use
/!repeat (.*?)/
and
/!(cmd1|cmd2|cmd3) (.*?)/
I don't understand exactly what you are hoping to match, but the lazy operator seems wrong for example
/!COMMAND (.*?)/ applied to !COMMAND paramater will match !COMMAND only, (.*?) at the end of a regex is guaranteed to match nothing.
You're doing one thing wrong.
If you replace your [] brackets with () everything should work. Between [] you put some letters to match. [abc] would match a, b, or c, not "abc", while (abc) would match "abc" and (abc|bca) would match "abc" or "bca".
Check out the Perl regular expressions tutorial and reference for more information.