Regular expression restrictions - html

I am new to regular expressions. I have a text field and I need to limit the input to numbers and one comma. I know how to limit to numbers. But I have another condition. If the user enters a comma, they must enter another number after that. There is no restriction on the number of digits. There can only be one comma. The following are all valid inputs:
123456
4567,8907
but I can't allow 4567,.
I have this pattern, pattern="[0-9]+([,][0-9]+)?"
but it's not working.

The following regex allows any number of digits (including none), followed optionally by a single comma, followed by any number of digits. This pattern must span from beginning to end using the ^ and $ symbols, so no other characters are allowed:
^[0-9]*,?[0-9]+$
Try here:
let inp = document.getElementsByTagName('input')[0];
let p = document.getElementsByTagName('p')[0];
inp.addEventListener('input', () => {
let result = inp.value.match(`^[0-9]*,?[0-9]+$`);
p.innerHTML = `Is "${inp.value}" allowed? ${result ? 'YES' : 'NO'}`;
});
body { font-family: monospace; }
<input type="text" placeholder="try a value here"/>
<p></p>

Related

How can i typetext of a number input in testcafe

when i try to typetext as string on an input with type=number the browser display different value
var items = ["15300", "06500", "15400","24500","30580","77104","92730"];
const zipcode =items[Math.floor(Math.random()*items.length)];
console.log(zipcode);
return String(zipcode)
}
.typeText(AddressesLocators.txt.txtZipcode,await Utils.getRandomZipcode(),{ replace: Boolean })
This is because when setting the "type" attribute of an HTML input element to "number", the browser expects the entered value to be a valid number and therefore may attempt to convert the entered value to a number.
If you enter a string like "15300" in an input with type="number", the browser will try to convert the string to a number, in which case the resulting value will be 15300. However, if you enter a string like "06500 ", the browser will interpret the value as an octal number and then convert it to decimal, which will result in 3328 instead of 6500.
To avoid this behavior, you can use an input with type="text" instead of type="number" if you want the entered value to be treated as a string. Or, if you need to use type="number", make sure the value you enter is a valid number and does not start with a leading zero, such as "6500" instead of "06500".
I hope this helps.

How do I search for a string in this JSON with Python

My JSON file looks something like:
{
"generator": {
"name": "Xfer Records Serum",
....
},
"generator": {
"name: "Lennar Digital Sylenth1",
....
}
}
I ask the user for search term and the input is searched for in the name key only. All matching results are returned. It means if I input 's' only then also both the above ones would be returned. Also please explain me how to return all the object names which are generators. The more simple method the better it will be for me. I use json library. However if another library is required not a problem.
Before switching to JSON I tried XML but it did not work.
If your goal is just to search all name properties, this will do the trick:
import re
def search_names(term, lines):
name_search = re.compile('\s*"name"\s*:\s*"(.*' + term + '.*)",?$', re.I)
return [x.group(1) for x in [name_search.search(y) for y in lines] if x]
with open('path/to/your.json') as f:
lines = f.readlines()
print(search_names('s', lines))
which would return both names you listed in your example.
The way the search_names() function works is it builds a regular expression that will match any line starting with "name": " (with varying amount of whitespace) followed by your search term with any other characters around it then terminated with " followed by an optional , and the end of string. Then applies that to each line from the file. Finally it filters out any non-matching lines and returns the value of the name property (the capture group contents) for each match.

Logstash - Substring from CSV column

I want to import many informations from a CSV file to Elastic Search.
My issue is I don't how can I use a equivalent of substring to select information into a CSV column.
In my case I have a field date (YYYYMMDD) and I want to have (YYYY-MM-DD).
I use filter, mutate, gsub like:
filter
{
mutate
{
gsub => ["date", "[0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789]", "[0123456789][0123456789][0123456789][0123456789]-[0123456789][0123456789]-[0123456789][0123456789]"]
}
}
But my result is false.
I can indentified my string but I don't how can I extract part of this.
My target it's to have something like:
gsub => ["date", "[0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789]","%{date}(0..3}-%{date}(4..5)-%{date}"(6..7)]
%{date}(0..3} : select from the first to the 4 characters of csv columns date
You can use ruby plugin to do conversion. As you say, you will have a date field. So, we can use it directly in ruby
filter {
ruby {
code => "
date = Time.strptime(event['date'],'%Y%m%d')
event['date_new'] = date.strftime('%Y-%m-%d')
"
}
}
The date_new field is the format you want.
First, you can use a regexp range to match a sequence, so rather than [0123456789], you can do [0-9]. If you know there will be 4 numbers, you can do [0-9]{4}.
Second, you want to "capture" parts of your input string and reorder them in the output. For that, you need capture groups:
([0-9]{4})([0-9]{2})([0-9]{2})
where parens define the groups. Then you can reference those on the right side of your gsub:
\1-\2-\3
\1 is the first capture group, etc.
You might also consider getting these three fields when you do the grok{}, and then putting them together again later (perhaps with add_field).

Carriage return in String Tokenizer in Java

If I only specify carriage return (\r) in the String Tokenizer like this:
StringTokenizer st1 = new StringTokenizer(line,"\r");
where 'line' is the input string.
When I provide the following text as input:
Hello
Bello
Cello
ie. with two carriage return. (I press 'Enter'after Hello and Bello.)
But the output of this is 3 in System.out.println(st1.countTokens());
Is there an explanation?
When you split a string using a separator, then, provided that your separator occurs n times, the number of elements after the split will be n+1. Look at this visual example, using comma as separator:
text1,text2,text3,text4
It will yield 4 results
Look at another example:
text1,text2,text3,
It will yield 4 results as well, the last being an empty string.

Accept String Parameter in Yii2 Action

In my config file I've set the url rule like this :
<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>
And what happens is:-
controller/action/123 (work)
controller/action/hello (not work)
But it accepts only digit as the parameter.
What I want is that both digit and string should be accepted.
Please help!!!!
The d+ pattern matches numbers 0-9, so it is working as expected. Change the regex pattern to match strings. Try w+.
Change:
<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>
To:
<controller:\w+>/<action:\w+>/<id:\w+>' => '<controller>/<action>