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

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.

Related

Regex to match forward slash surrounded by double quotes

I have a serialised string that comes from Spring hosted end-points. On the frontend which is javascript based, I wanted to prettify the serialised string that comes from API to a string that is parsable through JSON.parse();
Please let me know the regex to match and replace the required fields as below.
sample string: \"address\":\"<VALUE>"\"}, I want to replace all the instances of "\" which comes at the end of VALUE with \"
Tried doing this: str.replaceAll('\"/\\\"', '/\\\"') but no luck.
Here is the code, we have to escape characters to put the wanted values into the variable:
testString='\\\"address\\\":\\\"<VALUE>"\\\"},';
alert(testString);
alert(testString.replace(/\"\\\"/,'\\\"'));
The first alert gives us the originale testString:
\"address\":\"<VALUE>"\"},
and the second the modified testString
\"address\":\"<VALUE>\"},
Tested with https://www.webtoolkitonline.com/javascript-tester.html

How to extract FirstName and LastName from html tags with regex?

I have response body which contains
"<h3 class="panel-title">Welcome
First Last </h3>"
I want to fetch 'First Last' as a output
The regular expression I have tried are
"Welcome(\s*([A-Za-z]+))(\s*([A-Za-z]+))"
"Welcome \s*([A-Za-z]+)\s*([A-Za-z]+)"
But not able to get the result. If I remove the newline and take it as
"<h3 class="panel-title">Welcome First Last </h3>" it is detecting in online regex maker.
I suspect your problem is the carriage return between "Welcome" and the user name. If you use the "single-line mode" flag (?s) in your regex, it will ignore newlines. Try these:
(?s)Welcome(\s*([A-Za-z]+))(\s*([A-Za-z]+))
(?s)Welcome \s*([A-Za-z]+)\s*([A-Za-z]+)
(this works in jMeter and any other java or php based regex, but not in javascript. In the comments on the question you say you're using javascript and also jMeter - if it is a jMeter question, then this will help. if javaScript, try one of the other answers)
Well, usually I don't recommend regex for this kind of work. DOM manipulation plays at its best.
but you can use following regex to yank text:
/(?:<h3.*?>)([^<]+)(?:<\/h3>)/i
See demo at https://regex101.com/r/wA2sZ9/1
This will extract First and Last names including extra spacing. I'm sure you can easily deal with spaces.
In jmeter reg exp extractor you can use:
<h3 class="panel-title">Welcome(.*?)</h3>
Then take value using $1$.
In the data you shown welcome is followed by enter.If actually its part of response then you have to use \n.
<h3 class="panel-title">Welcome\n(.*?)</h3>
Otherwise above one is enough.
First verify this in jmeter using regular expression tester of response body.
Welcome([\s\S]+?)<
Try this, it will definitely work.
Regular expressions are greedy by default, try this
Welcome\s*([A-Za-z]+)\s*([A-Za-z]+)
Groups 1 and 2 contain your data
Check it here

Laravel Regex Validation (text should end up with .jpg or .png)

I have a laravel validation rule something like this
public function rules(){
return [
'title' => 'required|max:100',
'featured_image' => 'required|max:100|regex:(\d)+.(?:jpe?g|png|gif)',
];
}
I have a txt field where i dynamically add an image name, something like this (8123123123.jpg OR 234234234.png). If the text field doesn't have this pattern i want to show an error.
Now this regex does work in http://regexr.com/ but in laravel it doesn't. So basically it should look for digits as file name and should end with .jpg or .png
Any help is appreciated
use like this
'featured_image' => ['required', 'max:100', 'regex:/(\d)+.(?:jpe?g|png|gif)/']
You have to add a regex delimiter http://php.net/manual/en/regexp.reference.delimiters.php
According to the docs you should put your validations in a array:
regex:pattern
The field under validation must match the given regular expression.
Note: When using the regex pattern, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character.

Setting regular expression to validate URL format in Adobe CQ5

I want to validate a URL inside a textfield using Adobe CQ5, so I set up the properties regex and regexText as usual, but for some reason is not working:
<facebook
jcr:primaryType="cq:Widget"
emptyText="http://www.facebook.com/account-name"
fieldDescription="Set the Facebook URL"
fieldLabel="Facebook"
name="./facebookUrl"
regex="/^(http://www.|https://www.|http://|https://)[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(:[0-9]{1,5})?(/.*)?$/"
regexText="Invalid URL format"
xtype="textfield"/>
So when I type inside the component I can see an error message at the console:
Uncaught TypeError: this.regex.test is not a function
To be more accurate the error comes from this line:
if (this.regex && !this.regex.test(value)) {
I tried several regular expressions and none of them worked. I guess the problem is the regular expression itself, because in the other hand I have this other regex to evaluate email address, and it works perfectly fine:
/^[A-za-z0-9]+[\\._]*[A-za-z0-9]*#[A-za-z.-]+[\\.]+[A-Za-z]{2,4}$/
Any suggestions? Thanks in advance.
The syntax of your regex seems to treat the forward slashes (/) as special characters. Since you want to parse a URL containing slashes, my guess is you should escape them twice like this: '\\/' instead of '/'. The result would be:
/^(http:\\/\\/www.|https:\\/\\/www.|http:\\/\\/|https:\\/\\/)[a-z0-9]+([-.]{1}[a-z0-9]+)‌​*.[a-z]{2,5}(:[0-9]{1,5})?(\\/.*)?$/
You need to escape them twice because the string to be compiled as a regex must contain '\/' to escape the slashes, but to introduce a backslash in a string you have to escape the backslash itself too.

zend validator 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}/'));