How can I use RegEx to differentiate between screen resolutions? - regex

I'm trying to create a RegEx expression that will match a numeric range from 0 to 600 so I can easily differentiate between a small mobile device and tablets/desktops. I'm using Qualtrics' survey software to do the rest - all I need is the RegEx expression.
However, I'm not 100% sure how Qualtrics takes in the data. I believe it takes it in the following format:
360x640
320x568
320x480
1920x1080
360x640
1280x800
320x568
1920x1080
360x640
1280x800
1920x1080
480x800
320x480
1280x800
1366x768
320x568
1280x800
Where I'm testing the FIRST number, e.g. the number before the 'x' character.
Here's some RegEx I've tried that did not work:
([0-9]{1,2}|[1-4][0-9]{2}|600)*x
That code recognizes numbers before the 'x', but it doesn't stop at 600 - it recognizes all numbers before the 'x' (e.g. from 000 to 9999).
How do I get the range I want? Please and thank you!
Note: I've tried using the RegEx number range generator here, but it doesn't work for what I want to accomplish.

I'd do:
\b(?:600|[1-9]\d?|[1-5]\d{2})x
Where:
\b is a word boundary, it makes sure there're no digits before
(?: ) is a non capture group
600 matches 600
[1-9]\d? matches number from 1 to 99
[1-5]\d{2} matches number from 100 to 599

I don't believe there are width lower than 100, so you can use this pattern:
^([1-5][0-9]{2}|600)x

You can use this regex but with m modifier if your input contains all those lines together:
^([0-5]\d{2}|600)x
Live demo

Related

Regex to remove number and sign '/' before or after '/' if less than 20 in productfeed attribute

We have created a product feed from a Magento webshop using the Koongo Module. We want to submit this feed to a marketplace. Our customer does not consistently fill in the size field when adding products. As a result, there is also an incorrect format for the marketplace. Size examples are: 39/6 or 40 / 6.5 or 8/42 or 8.5 / 42.5. In short, either first the European size and then the UK size or first the UK size and then the European size. We want to correctly display the size in the feed using a regular expression, namely only the European size. In short, we no longer want to include either '/ UK m' or 'UK size /'. It is important that the sign / must also be removed from the outcome in our product feed. Can you help?
We have the option to fill in two fields, namely 'Rewrite from' and 'Rewrite to'. We can use a regular expression for each of these fields.
Thanks in advance for the help.
It would be simple to only take the numbers >= 20. Does that help you?
[2-5][0-9](\.5)?
This will match all sizes between 20 and 59 with the option of half sizes
I am not familiar with Magento. If it supports regular expression replace with capture groups try this:
Rewrite From: ^.*?([234][0-9](\.5)?).*?$
Rewrite To: $1
Explanation:
^ - anchor at start of string
.*? - non-greedy scan
( - capture group start
[234][0-9] - number between 20 and 49
(\.5)? - optional .5 fraction
) - capture group end
.*? - non-greedy scan
$ - anchor at end of string
use capture group in replace: $1

Detecting whole number with an "x" or "-" after using regex

I'm trying to use regex to detect the quantity in a list of items on a receipt. The software uses OCR so the return can vary a bit. To help ive narrowed it to assume that the quantity will always be at the start of the line and is always a whole number. The use cases I'm trying to cover are:
2 Burgers $4.00
2 x Burgers $4.00
2 X Burgers $4.00
2x Burgers $4.00
2X Burgers $4.00
2- Burgers $4.00
2 - Burgers $4.00
The plan is for the regex to return 2 for each example above. The regex I have so far is \\d{1,2}(\\s[xX]|[xX]) this returns the top three examples fine but as much as I have tried I cant seem to get the rest detected, I haven't looked at adding the - yet as was stuck on detecting the x next to the Int.
Any help would be great, thanks
To help ive narrowed it to assume that the quantity will always be at the start of the line and is always a whole number.
I suggest using something like
let pattern = "(?m)^\\d+"
See the regex demo.
The pattern will match 1 or more digits at the start of any line:
(?m) - a MULTILINE modifier that makes ^ match the start of a line rather than the start of a string
^ - start of a line
\d+ - 1 or more (+) digits.
If you need to specify that some text should follow the digits, use a positive lookahead. E.g. you may require x/X/- after 0+ whitespaces, or a whitespace right after. Then, you need to use
let pattern = "(?m)\\d+(?=\\s*[xX-]|\\s)"
Here, (?=\\s*[xX-]|\\s) will make the regex match only those digits at the start of the line(s) that are immediately followed with either 0+ whitespace chars and then X, x or -, or that are immediately followed with a whitespace.
See this regex demo.
^(\\d+)\\s?[xX-]?.*?([$£](?:\\d{1,2})(?:,?\\d{3})*\.?\\d{0,2})$
See it working here (extra backslashes have been added in the code above to allow it to work in Swift, whereas the below link shows the expected result in JS, Python, Go and PHP, which means there are less backslashes there).
Will capture number of items and the price, what the item is is not captured.

Regex - Alteryx - Parse - How to find an expression starting by the end of the string

I need to parse the following expression:
Fertilizer abc 7-15-15 5KG BOX 250 KG
in 3 fields:
The product description: Fertilizer abc 7-15-15
Size: 250
Size unit: KG
Do not know how to proceed. Please, any help and explanation?
Try this in the alteryx REGEX Tool with Parse selected as the Method:
([A-z ]* [\d-]{6,8}) ([A-Z\d]{2,6}) (.{1,5}?) (\d*) ([A-Z]*)
You can test it at Regexpal to see the breakdown of each group but essentially the first set of brackets will get you your product description (text and spaces until 6-8 characters made up of digits and dashes), the 2nd & 3rd parts will deal with the erroneous info that you don't want, the 4th group will be just digits and the 5th group will be any text afterwards.
Note that this will change dramatically if your data has digits where there is characters currently etc.
You can always break it up into even smaller groups and then concatenate back together as well.

my first lookahead (or lookbehind?) exercise

8:9 DAR4:3], 23.98 fps
8:9 DAR4:3], 29.97 fps
8:9 DAR4:3], 25 fps
I've got these 3 possible strings, and I'm looking for an expression that will extract the numbers which occur between the first comma in this line, and the " fps".
either 23.98, 29.97, 25
How best to attack this? Looking behind to whatever digits and dots [\d.] fall before " fps" seems the most logical way but I've never done one of these.
An example, please?
I have this already,
\d{2}\.\d{2}|25
but I'm afraid the "25" could pick up false positives from somewhere later in the text.
,\s*(\d+\.?\d*)\s*fps$
Edit live on Debuggex
Maybe this will work?
, indicates the the comma
\s* indicates zero or more spaces.
(\d+\.?\d*) captures decimal values in a capture group
\s* indicates zero or more spaces.
fps$ checks to see if fps is at the end of the line.
(?<=, )[0-9]+(\.[0-9]+)?(?= fps)
should work fine and will only match your numbers.
Regex101 demo

Regex Numerical Range 1 - 1 million

I'm looking for a expression range for monetary purposes. It needs to be 1 - 1 million and allow commas and periods. I don't need a min/max of (, and .) for correct formatting but I would like the digits after a period to be a min/max of 2 for actual cent values. Thanks
In Range:
640 or 5,000.35 or 999,000
Not in Range:
01 or 1,000,000.01 or 333,567.678
What I would suggest is :
Use something like that to verify that the input has a specific format :
(here's a demo - http://regexr.com?30l28)
(1[\.,])?([0-9]{1,3}[\.,])?([0-9]{1,3})([\.,][0-9]{1,2})
And then test the value range :
is value<1.000.000?
My regex is by no means 100% complete, but it DOES verify your general number format though.
This should do it:
^(1(\.\d{2})?|[1-9]\d{0,2}(,?\d{3})?(\.\d{2})?)|1((,000){0,2}|(000){0,2})(\.00)?$
But it would probably easier if you normalize the value first (e. g. remove any character except digits and the .) and then parse it.