zend validator regex - regex

I'm trying to use a regex validator on a zend form element like this-
$textarea = $this->createElement('text','scores');
$textarea->setLabel('Enter a comma separated list of numbers');
$textarea->setDecorators(
array('ViewHelper',
array('HtmlTag',
array('tag' => 'div',
'class'=>'scores'
)
)
)
);
$textarea->addDecorator('Label')
->setRequired(true)
->addFilter(new Zend_Filter_StringTrim())
->addValidator('regex',true,array('^\d{1,3}([,]\d{1,3})*$'))
->addErrorMessage('Please enter a comma separated list of numbers');
I'm just trying to validate that the text area contains a list of comma separated numbers.
Currently im getting "Internal error while using the pattern '^\d{1,3}([,]\d{1,3})*$'".
I guess there's something wrong with the regex?
Any help would be appreciated :)
thanks,
pete

Try escaping the backslashes:
'^\\d{1,3}(,\\d{1,3})*$'
You don't need the brackets around the comma.
Also, you might want to allow whitespace between the numbers and separators:
'^\\s*\\d{1,3}(\\s*,\\s*\\d{1,3})*\\s*$'

You need add symbols for start and end regexp. For example:
->addValidator('regex',true,array('#^\\d{1,3}([,]\\d{1,3})*$#'))

true you need delimiters. but don't escape your slashes :)

IMHO you are missing slash "/" at the end of your regex. I'm not an expert but this is working for me:
->addValidator(new Zend_Validate_Regex('/^[a-zA-Z0-9][a-zA-Z0-9 ._-]{1,31}/'));

Related

Regex Adding a URL path except the current one I'm at

I'm trying to add something along the lines of this regex logic.
For Input:
reading/
reading/123
reading/456
reading/789
I want the regex to match only
reading/123
reading/456
reading/789
Excluding reading/.
I've tried reading\/* but that doesn't work because it includes reading/
You must escape your backslashes in Hugo, \\/\\d+.

JSF validateRegex do not work with '#,$,%,(),?,!'

I am trying to validate a TextArea{basically a Comments box} which would accept almost all characters and special characters with spaces included. The constraint is i only want to use JSF validateRegex tag. I do not want to use validator java class.
IN my code i have done like below
<f:validateRegex pattern="^[a-zA-Z0-9\s_\s-.]*[##$%!]*$">
The textarea does not accept any string with '#,$,%,(),?,!' inspite of including in regex But it accepts only '#'. Can anyone help me out in creating a valid regular expression for this textarea field in JSF.
This accepts what you want. Any combination of letters, numbers, whitespace, underscore, minus, ".", "#", "#", "$", "#" and "!".
^[\w\s\-.\##\$%!]*$
If you also want "?", "(" and ")" to validate use this:
^[\w\s\-.\##\$%!\?\(\)]*$
your regex is fine and you can test it in this site regexTester, please write your full code for ability to help you.

Regex: Get subtext from a string

I have a list of text lines. Each line contains a title and a URL as follows:
product-title-7134 http://domain.com/page-1
another-product-title-822 http://domain.com/page-218
etc.
Using only .NET regex, please help me extract the url from each line.
I understand it can be done by looking at the string from the end until the http is met and output that part but I don't know the exact regex formula for that. Any help is much appreciated.
I would do that with this regex:
http://(\S+)
And find first group in every match.
This regex will math all https:// and http:// links:
(http|https)(://\S+)
You can test this in the .NET regex tester: http://regexstorm.net/tester

Regex to match any character or fullstop?

I'm trying to create a regex that takes a filename like:
/cloud-support/filename.html#pagesection
and redirects it to:
/cloud-platform/filename#pagesection
Could anyone advise how to do this?
Currently I've got part-way there, with:
"^/cloud-support/(.*)$" => "/cloud-platform/$1",
which redirects the directory okay - but still has a superfluous .html.
Could I just match for a literal .html with optional #? How would I do that?
Thanks.
Maybe something like this:
"^/cloud-support/(.*?)(\.html)?(#.+)$" => "/cloud-platform/$1$3"
where the first group is a non-greedy match (.*?)
"^/cloud-support/(\w+).html(.*)" => "/cloud-platform/$1$2"
Would something like this work?
"^/cloud-support/([^.]+)[^#]*(.*)$" => "/cloud-platform/$1$2"
Can you try the regex
"^/cloud-support/(.*)\.html(#.*)?$"
The \.html part matches .html while (#.*)? allows an optional # plus something.

RegEx to exclude a string

I have the following strings in my application.
/admin/stylesheets/11
/admin/javascripts/11
/contactus
what I want to do is to write a regular expression to capture anything other than string starting with 'admin'
basically my regex should capture only
/contactus
by excluding both
/admin/stylesheets/11
/admin/javascripts/11
to capture all i wrote
/.+/
and i wrote /(admin).+/ which captures everything starts with 'admin'. how can i do the reverse. I mean get everything not starting with 'admin'
thanks in advance
cheers
sameera
EDIT - Thanks all for the answers
I'm using ruby/ Rails3 and trying to map a route in my routes.rb file
My original routes file is as followss
match '/:all' => 'page#index', :constraints => { :all => /.+/ }
and i want the RegEx to replace /.+/
thanks
If the language/regular expression implementation you are using supports look-ahead assertions, you can do this:
^/(?!admin/).+/
Otherwise, if you only can use basic syntax, you will need to do something like this:
^/([^a].*|a($|[^d].*|d($|[^m].*|m($|[^i].*|i($|[^n].*)))))