Cant do work Propel Match validators - preg-match

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 ]*$

Related

IIS Url rewrite rule with request max lenght

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}

How to make a leading token optional in a RegexNER expression?

I have a very simple use case where I need to add an NER annotation to a sequence of two words where the first word is optional.
For example, I need to annotate both "net income" and "income" phrases as a same NE type.
With ordinary regular expressions the following expression works:
([Nn]et\s)?[Ii]ncome
However, in RegexNER it does not work.
The effect that the above regex has in RegexNER is that the word "income" is annotated in both sequences, but the word "net" is not annotated in the sequence "net income", which is not the result that I need.
That is sort of expected, knowing that RegexNER matches a sequence of regular expressions over a sequence of tokens, not a single regular expression over a single string.
However, the following syntax does not work either:
([Nn]et)? [Ii]ncome
The effect that this expression has is that the sequence "net income" is annotated entirely, but just "income" is not annotated at all.
This is unexpected, since this seems like a very simple use case.
I tried different ways to denote the initial token as a group and also tried different quantifiers - it still does not work.
Any help with making the first token optional will be appreciated.
Let me answer my own question. This is not a direct solution, it's a workaround.
The following expression will work, but only with TokensRegex, not with RegexNER:
/[Nn]et/? /[Ii]ncome/
I am not sure why this is the case, maybe RegexNER does not support quantifiers at the token level the same way TokensRegex does.

How to visit a link inside an email using capybara

I am new to cucumber with capybara. I got an application to test whose flow is:'after submitting a form, an email will be sent to the user which contains the link to another app. In order to access the app we have to open the mail and click the link, which will redirect to the app.'. I don't have access to the mail Id. Is there any way to extract that link and continue with the flow?
Please, give some possible way to do it.
Regards,
Abhisek Das
In your test, use whatever means you need in order to trigger the sending of the email by your application. Once the email is sent, use a regular expression to find the URL from the link within the email body (note this will work only for an email that contains a single link), and then visit the path from that URL with Capybara to continue with your test:
path_regex = /(?:"https?\:\/\/.*?)(\/.*?)(?:")/
email = ActionMailer::Base.deliveries.last
path = email.body.match(path_regex)[1]
visit(path)
Regular expression explained
A regular expression (regex) itself is demarcated by forward slashes, and this regex in particular consists of three groups, each demarcated by pairs of parentheses. The first and third groups both begin with ?:, indicating that they are non-capturing groups, while the second is a capturing group (no ?:). I will explain the significance of this distinction below.
The first group, (?:"https?\:\/\/.*?), is a:
non-capturing group, ?:
that matches a single double quote, "
we match a quote since we anticipate the URL to be in the href="..." attribute of a link tag
followed by the string http
optionally followed by a lowercase s, s?
the question mark makes the preceding match, in this case s, optional
followed by a colon and two forward slashes, \:\/\/
note the backslashes, which are used to escape characters that otherwise have a special meaning in a regex
followed by a wildcard, .*?, which will match any character any number of times up until the next match in the regex is reached
the period, or wildcard, matches any character
the asterisk, *, repeats the preceding match up to an unlimited number of times, depending on the successive match that follows
the question mark makes this a lazy match, meaning the wildcard will match as few characters as possible while still allowing the next match in the regex to be satisfied
The second group, (\/.*?) is a capturing group that:
matches a single forward slash, \/
this will match the first forward slash after the host portion of the URL (e.g. the slash at the end of http://www.example.com/) since the slashes in http:// were already matched by the first group
followed by another lazy wildcard, .*?
The third group, (?:"), is:
another non-capturing group, ?:
that matches a single double quote, "
And thus, our second group will match the portion of the URL starting with the forward slash after the host and going up to, but not including, the double quote at the end of our href="...".
When we call the match method using our regex, it returns an instance of MatchData, which behaves much like an array. The element at index 0 is a string containing the entire matched string (from all of the groups in the regex), while elements at subsequent indices contain only the portions of the string matched by the regex's capturing groups (only our second group, in this case). Thus, to get the corresponding match of our second group—which is the path we want to visit using Capybara—we grab the element at index 1.
You can use Nokogiri to parse the email body and find the link you want to click.
Imagine you want to click a link Change my password:
email = ActionMailer::Base.deliveries.last
html = Nokogiri::HTML(email.html_part.body.to_s)
target_url = html.at("a:contains('Change my password')")['href']
visit target_url
I think this is more semantic and robust that using regular expressions. For example, this would work if the email has many links.
If you're using or willing to use the capybara-email gem, there's now a simpler way of doing this. Let's say you've generated an email to recipient#email.com, which contains the link 'fancy link'.
Then you can just do this in your test suite:
open_email('recipient#email.com') # Allows the current_email method
current_email.click_link 'fancy link'

Spring Expression Language equivalent for \r, \n, \t etc

I am using Spring Integration. I get a string (payload) like below:
<Element>
<Sub-Element>5</Sub-Element>
</Element>
I need to test if above string starts with <Element><Sub-Element> which is actually <Element>\r\n <Sub-Element>.
<int:recipient-list-router id="customRouter" input-channel="routingChannel">
<int:recipient channel="channel1" selector-expression="payload.startsWith('<Element><Sub-Element>')"/>
<int:recipient channel="channel2" selector-expression="!payload.startsWith('<Element><Sub-Element>')"/>
</int:recipient-list-router>
Ideally the first router should pass the test but in this case its failing. Can anyone help me finding out what is the SpEL equivalent of \r \n etc ?
SpEL doesn't have escapes for those, but you can use regular expressions to do the selection...
<recipient selector-expression="payload matches '<Element>\r\n<Sub-Element>.*'" channel="channel1"/>
<recipient selector-expression="!(payload matches '<Element>\r\n<Sub-Element>.*')" channel="channel2"/>
If you are not familiar with regex, the .* at the end matches anything (hence this regex is the equivalent of startsWith()).
EDIT:
While this will work, I feel I should point out that relying on specific values in insignificant white space in XML documents is brittle - if the client changes to use, say \n instead, or even no whitespace, your application will break. You should consider using something like an <int-xml:xpath-router/> instead.
Thanks Gary.
So the working list-recipient-router looks like
Either
<recipient selector-expression="payload matches '(?s)<Element>(\s*)<Sub>(.*)'" channel="channel1"/>
<recipient selector-expression="!(payload matches '(?s)<Element>(\s*)<Sub>(.*)')" channel="channel2"/>
Or
<recipient selector-expression="payload matches '(?s)<Element>(.*)<Sub>(.*)'" channel="channel1"/>
<recipient selector-expression="!(payload matches '(?s)<Element>(.*)<Sub>(.*)')" channel="channel2"/>
May keep captures () or may not. Both works.

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