I googled a lot, but I'm stuck.
There is a cool thing in HTML5, required patterns. It's great for emails / phones / dates validation. I use it in my small project for checking numbers. What I need is a pattern for:
YYYY.ordernumber
Order number may be any number from 1 to 1000000.
I tried to modify some YYYY.MM patterns for my case, but with no luck. What ever I type in does not pass the validation.
Can anyone please help?
UPDATE: Added a lookahead to ensure 'ordernumber' is > 0 (thanks to M42's remark in comments).
You can use those two attributes with your <input>:
pattern="^[0-9]{4}\.(?!0+$)([0-9]{1,6}|1000000)$"
required
E.g.
<input type="text" placeHolder="YYYY.ordernumber" title="YYYY.ordernumber"
pattern="^[0-9]{4}\.(?!0+$)([0-9]{1,6}|1000000)$" required />
See, also, this short demo.
Short explanation of the regex:
^[0-9]{4}\.(?!0+$)([0-9]{1,6}|1000000)$ _____________
^\______/\/\_____/ \________/\______/ ^___|match the end|
| | | |_(*2) |_ |_____ |of the string|
_______| | |____ | |
_________|__ _______|_____ _|______ _|________ _|______
|match the | |match exactly| |match a | |match 1 to| |or match|
|beggining of| |4 digits | |dot (*1)| |6 digits | |1000000 |
|the string |
(*1): '.' is a special character in regex, so it has to be escaped ('.').
(*2): This is a negative lookahead which does consume any characters, but looks ahead and makes sure that the rest of the string in not consisted of zeros only.
Just for the sake of completeness:
I must point out the fact that [0-9] matches only digits 0-9. If you need to also match other digit characters, such as for example Eastern Arabic numerals (٠١٢٣٤٥٦٧٨٩), you can use \d instead.
Related
I have to extract campaign names from this text:
[Ads] | [Bing] | [Leaderboard] | UCL MATCH | 29 September - 31 Desember 2019
Ideally, I only want to extract UCL MATCH and remove all the others, how do I do this with regex? Or is there some other way to do it on Google Sheets?
I only managed to do this:
=REGEXEXTRACT(K8,"\[(.*)\]\ | \ w+\|\[(.+)\]\|")
which resulted in Ads] | [Bing] | [Leaderboard.
Please read my comment to your original post.
However, assuming that your answer to all questions in that comment is YES, this should work:
=TRIM(REGEXEXTRACT(K8,"([^\|]+)\|[^\|]+$"))
You can extract the 4th pipe-separated item with
=REGEXEXTRACT(K8,"^(?:[^|]*\|){3}\s*([^|]*[^|\s])")
See the regex demo. Details:
^ - start of string
(?:[^|]*\|){3} - three sequences of zero or more chars other than | and then a | char
\s* - zero or more whitespaces
([^|]*[^|\s]) - Group 1 (the actual return value): zero or more chars other than | and then a char other than whitespace and | char.
try:
=INDEX(TRIM(SPLIT(A1; "|"));;4)
for array:
=INDEX(TRIM(SPLIT(A1:A; "|"));;4)
I'm trying to write a regex that checks if string contains 6 or more signs including 1 or more special sign [^0-9a-zA-Z\s] and 1 or more [0-9a-zA-Z].
Spent like 2h and not getting any closer :/
maybe this is of some help:
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).{6,13}$
Password expresion that requires one lower case letter, one upper case letter, one digit, 6-13 length, and no spaces.
Matches:
1agdH*$# | 1agdC*$# | 1agdB*$#
Non-Matches:
wyrn%#*&$# f | mbndkfh782 | BNfhjdhfjd&*)%#$)
This is based on the Regex Lib entry here
Taking the style of Hasson's answer . . .
grep -P '^(?=.*[^a-zA-Z0-9\s])(?=.*[a-zA-Z0-9])(?!.*\s).{6}'
6 or more chars (regexp not ended with $)
1 or more special char (?=.*[^0-9a-zA-Z\s])
1 or more (?=.*[0-9a-zA-Z])
no whitespace (?!.*\s)
Some test data, NO match:
password
pa5sword
pa5sWord
pa5sWord
password
test
1agdA
1agd
wyrn%#*&$# f
mbndkfh782
t1*$
Some test data, YES match:
pa5*Word
pa5*Word
pa5*Word1
pa5*Wor
1agdA*
1agdA*$
1agdA*$#
1agdA*$#1
1agdA*$#12
1agdA*$#123
1agdA*$#a
1agdA*$#ab
1agdA*$#abc
1agdA*$#abcd
BNfhjdhfjd&*)%#$)
I'm looking for a way to construct a long regex expression that includes several items with special chars that are regex commands as well, for example:
"Bath Tub | Black" and "Bath Tub | Green".
How do I use them as "string" with the | inside?
I thought about
(Bath Tub|Showers) \| (Black|Green)
but then again there are items like "Bath Tub | Green (light)" and "Bath Tub | Green (dark)" so I need to add that too, so I thought in some cases it would be the best solution to just take it like
"Bath Tub | Green (light)"|"Bath Tub | Green (dark)"
if that would work.
Any idea how to do it? I'm really horrible with regex and it is giving me a lot of headache, so I would appreciate any help!
Kind regards
You can add them all into alternation list:
Bath Tub|Showers|Black|Green(?: \((?:light|dark)\))?
Have a look at the example.
The main point is that all of these alternatives can be added to the list (separated with |), and in case you have additional words after these strings, you can add them as optional non-capturing groups ((?: \((?:light|dark)\))?). Also, pay attention at the escaped round brackets - they will be matched as literals.
If you want to match a literal pipe symbol inside a string |, you should escape it: \|. See this example.
To match all entries from "Bath Tub | Green (light)"|"Bath Tub | Green (dark)", together with quotes, you can add optional groups with quotes at start and end of the pattern: (?:")?(?:Bath Tub|Showers|Black|Green(?: \((?:light|dark)\))?)(?:")?. See yet another example.
I am looking at regexes to validate and parse well-known text, which is a format used to transfer spatial data and looks like:
POLYGON((51.124 -3.973, 51.1 -3.012, ....))
or
MULTIPOLYGON(((POLYGON((51.124 -3.973, 51.1 -3.012, ....)),POLYGON((50.14 -13.973, 51.1 -13.012, ....))
among other variations.
There is a good answer here: Parsing a WKT-file which uses the regex:
\d+(?:\.\d*)?
From other places I have also seen
\d*\.\d+|\d+
and
(\d*\.)?\d+
These all seem to do the same thing, but it got me wondering about the relative workings of these 3 regexes, and if there are any performance issues or subtleties under the hood to be aware of.
To be clear, I am aware that there are libraries for parsing WKT in various languages. My question is purely about the relative behavior of number extracting regexes.
It depends what number formats you need to allow, example:
format 1: 22
format 2: 22.2
format 3: .2
format 4: 2.
the 1st pattern \d+(?:\.\d*)? matches 1,2,4
the 2nd pattern \d*\.\d+|\d+ matches 1,2,3
the 3rd pattern (\d*\.)?\d+ matches 1,2,3 (and have an uneeded capturing group)
Note: pattern 2 and 3 are slower to succeed than the first if the number is an integer, because they must match all digits until the dot, backtrack to the start and retry the same digits one more time. (see the schema below)
str | pattern | state
-----+----------------+-----------------------------
123 | \d*\.\d+|\d+ | START
123 | \d*\.\d+|\d+ | OK
123 | \d*\.\d+|\d+ | OK
123 | \d*\.\d+|\d+ | OK
123 | \d*\.\d+|\d+ | FAIL => backtrack
123 | \d*\.\d+|\d+ | FAIL => backtrack
123 | \d*\.\d+|\d+ | FAIL => backtrack
123 | \d*\.\d+|\d+ | go to the next alternative
123 | \d*\.\d+|\d+ | OK
123 | \d*\.\d+|\d+ | OK
123 | \d*\.\d+|\d+ | OK => SUCCESS
if you want to match the four cases, you can use:
\.\d+|\d+(?:\.\d*)?
(+) if the number doesn't begin with a dot, the first alternative fails immediatly and the second alternative will match all other cases. The backtracking is limited to the minimum.
(-) if you have few numbers that start with a dot the first alternative will be tested and will fail each times. However, the first alternative fails quickly.(in other words, for the same reason). In this case, it is better to use \d+(?:\.\d*)?|\.\d+
Obviously, if you want to support negative values you need to add -?:
-?(?:\.\d+|\d+(?:\.\d*)?)
I am working on a Rails 3 application that needs to validate the password based on the following criteria: must be at least 6 characters and include one number and one letter.
Here is my Regex:
validates :password, :format => {:with => /^[([a-z]|[A-Z])0-9_-]{6,40}$/, message: "must be at least 6 characters and include one number and one letter."}
Right now if I put in a password of (for ex: dogfood) it will pass. But what I need it to do is to pass the criteria above.
I am not all that great at regex, so any and all help is greatly appreciated!
Use lookahead assertions:
/^(?=.*[a-zA-Z])(?=.*[0-9]).{6,}$/
| | |
| | |
| | Ensure there are at least 6 characters.
| |
| Look ahead for an arbitrary string followed by a number.
|
Look ahead for an arbitrary string followed by a letter.
Technically in this case you don't need the anchors, but it's good habit to use them.