IIS Url rewrite rule with request max lenght - windows

Based on this question:
IIS Rewrite rule based on length param
In windows server 2012 IIS, I would like to create a rule that check for the first parameter only for "n" max digits.
the answer proposed gives me an error,
<match url="product\/([A-Za-z0-9]{4,100}+)\/$" />
throws this error:
The expression contains a repeat expression (one of '', '?', '+', '{'
in most contexts) that is not preceded by an expression.
someone else have an idea to resolve?
Thank you

Your regexp should be like that: product\/([A-Za-z0-9]{4,100})\/$
You shouldnt use + if you specified length {4,100}

Related

preg_match(): Compilation failed: invalid range in character class

I'm a beginner for laravel an vuejs, when I am trying to refresh the page I got such error message, this is my code from web file :
Route::get('{path}',"HomeController#index")->where('path','(-a-z0-9_\s)');
Anyone with and idea?
From looking at you Regular Expression, even it it compiled you probably won't find any URI that matches it, so you will get Laravel No Route exception.
You should probably use Brackets [] to setup the range of character and add the + to match that range multiple times to make words or phrase. Use this Regex :
[-a-z0-9_\s]+
The Route codes :
Route::get('{path}',"HomeController#index")->where('path','[-a-z0-9_\s]+');
Or use Brackets inside the Parenthesis :
Route::get('{path}',"HomeController#index")->where('path','([-a-z0-9_\s]+)');
Use https://regex101.com/ to validate your string with Regex the next time
you just need to change your code like this
Route::get('{path}',"HomeController#index")->where('path','[-a-z0-9_\s]+');

how to test my Grammar antlr4 successfully? [duplicate]

I have been starting to use ANTLR and have noticed that it is pretty fickle with its lexer rules. An extremely frustrating example is the following:
grammar output;
test: FILEPATH NEWLINE TITLE ;
FILEPATH: ('A'..'Z'|'a'..'z'|'0'..'9'|':'|'\\'|'/'|' '|'-'|'_'|'.')+ ;
NEWLINE: '\r'? '\n' ;
TITLE: ('A'..'Z'|'a'..'z'|' ')+ ;
This grammar will not match something like:
c:\test.txt
x
Oddly if I change TITLE to be TITLE: 'x' ; it still fails this time giving an error message saying "mismatched input 'x' expecting 'x'" which is highly confusing. Even more oddly if I replace the usage of TITLE in test with FILEPATH the whole thing works (although FILEPATH will match more than I am looking to match so in general it isn't a valid solution for me).
I am highly confused as to why ANTLR is giving such extremely strange errors and then suddenly working for no apparent reason when shuffling things around.
This seems to be a common misunderstanding of ANTLR:
Language Processing in ANTLR:
The Language Processing is done in two strictly separated phases:
Lexing, i.e. partitioning the text into tokens
Parsing, i.e. building a parse tree from the tokens
Since lexing must preceed parsing there is a consequence: The lexer is independent of the parser, the parser cannot influence lexing.
Lexing
Lexing in ANTLR works as following:
all rules with uppercase first character are lexer rules
the lexer starts at the beginning and tries to find a rule that matches best to the current input
a best match is a match that has maximum length, i.e. the token that results from appending the next input character to the maximum length match is not matched by any lexer rule
tokens are generated from matches:
if one rule matches the maximum length match the corresponding token is pushed into the token stream
if multiple rules match the maximum length match the first defined token in the grammar is pushed to the token stream
Example: What is wrong with your grammar
Your grammar has two rules that are critical:
FILEPATH: ('A'..'Z'|'a'..'z'|'0'..'9'|':'|'\\'|'/'|' '|'-'|'_'|'.')+ ;
TITLE: ('A'..'Z'|'a'..'z'|' ')+ ;
Each match, that is matched by TITLE will also be matched by FILEPATH. And FILEPATH is defined before TITLE: So each token that you expect to be a title would be a FILEPATH.
There are two hints for that:
keep your lexer rules disjunct (no token should match a superset of another).
if your tokens intentionally match the same strings, then put them into the right order (in your case this will be sufficient).
if you need a parser driven lexer you have to change to another parser generator: PEG-Parsers or GLR-Parsers will do that (but of course this can produce other problems).
This was not directly OP's problem, but for those who have the same error message, here is something you could check.
I had the same Mismatched Input 'x' expecting 'x' vague error message when I introduced a new keyword. The reason for me was that I had placed the new key word after my VARNAME lexer rule, which assigned it as a variable name instead of as the new keyword. I fixed it by putting the keywords before the VARNAME rule.

Regular Expression - matching file extension from a URL

So I have a very specific URL, that tends to always follow the following format:
http://mtc.cdn.vine.co/r/videos/0DCB6FF2EF1179983941847883776_38a153447e7.1.5.3901866229871838946.mp4?versionId=.k9_w6W7t1Yr1KUCWRIm6AnYhSdOUz32
Basically I want to grab everything from after the . and before the ?versionId as I imagine that's the consistent location of the file extension.
I currently have something like this where \.\.{0}(.+)\?versionId it is matching everything starting from the first . to versionId.
One solution I thought about doing was using the . as a delimiter. I've never tried to restrict a character, but basically I would want it to try to match everything starting with a ., reject anything that has a . leading up to the ?.
Anyone got any idea how to get this to work?
Is your goal to get 'mp4'? Might consider not using a regex at all...
> require 'uri'
> uri = URI.parse('http://mtc.cdn.vine.co/r/videos/0DCB6FF2EF1179983941847883776_38a153447e7.1.5.3901866229871838946.mp4?versionId=.k9_w6W7t1Yr1KUCWRIm6AnYhSdOUz32')
=> #<URI::HTTP http://mtc.cdn.vine.co/r/videos/0DCB6FF2EF1179983941847883776_38a153447e7.1.5.3901866229871838946.mp4?versionId=.k9_w6W7t1Yr1KUCWRIm6AnYhSdOUz32>
> uri.path
=> "/r/videos/0DCB6FF2EF1179983941847883776_38a153447e7.1.5.3901866229871838946.mp4"
> File.extname(uri.path)
=> ".mp4"
Completely in agreement with Philip Hallstrom, this is a typical XY problem. However, if you really wish to just hone your Regexp skills, the literal solution to your question is (Rubular):
(?<=\.)[^.]+(?=\?)
"From where a period was just before, match any number of non-periods, matching up to where question mark is just after."
To understand this, read up on positive lookbehind ((?<=...)), positive lookahead ((?=...)), and negated character sets ([^...]).

Cant do work Propel Match validators

Im developing a CRUD application. I use Propel as ORM, and added validate rules into the schema.xml. But this, not work fine.
For example, i send a string that contains a user lastname, and the validator is:
<rule name='minLength' value='4'/>
<rule name='maxLength' value='30'/>
<rule name='notMatch' value='/^\s+$/' />
<!-- the name can be only chars and spaces -->
<rule name='match' value='/[^A-Za-z ]$/'/>
The user lastname sent was: 'Martinez D Elia'. And the valid fails on 4th rule.
Any idea ?.
Slight change to the previous answer, since you want a minimum of 4 characters and a max of 30, you may want to use this:
/^[A-Za-z ]{4,30}$/
Also note that you do need the "/" before and after the regular expression. And FYI, your regex was wrong for two reasons: the placement of the "^" inside of the character class and the missing length qualifier after the character class (without either the "*", "+", or brackets {}, you only match one single characters, any more will fail).
The regex is wrong. The correct match is:
^[A-Za-z ]*$

Getting last part of URL into variable with Url Rewriter

I'm using Url Rewriter to create user-friendly URLs in my web app and have the following rule set up
<rewrite url="/(?!Default.aspx).+" to="/letterchain.aspx?ppc=$1"/>
How do I replace $1 so that it is the last part of the URL?
So that the following
www.mywebapp.com/hello
would transform to
/letterchain.aspx?ppc=hello
I've read the docs but can't find anything.
The $1 in the to portion of the group refers to the first capture group defined (eg the part in the brackets).
The part that you actually want injecting into the $1 is the .+ which isnt in a capture group.
I'm not sure but I think because of the (?! ) "match if suffix is absent" query this isnt counted as numbered capture group $1 so this should work:
<rewrite url="/(?!Default.aspx)(.+)" to="/letterchain.aspx?ppc=$1"/>
If it doesnt then just try inserting the second capture group into your to string instead:
<rewrite url="/(?!Default.aspx)(.+)" to="/letterchain.aspx?ppc=$2"/>
Please note that if you are developing for IIS 7+ http://www.iis.net/download/urlrewrite/ is a module from Microsoft that performs faster rewrites with lower footprint.
BTW, your regex has a small problem, you need to escape the dot character, that is "/(?!Default.aspx)(.+)"

Resources