Regex for only allowing letters, numbers, and commas? - regex

I have a label name called "Color Scheme" and an input showing you the HEX colors that you just inputted.
I have limited the max length to 27 so when they input their HEX colors, like this 222222,000000,999999,ffeb00. By default, I put the commas in myself, but I want the regex to automatically input a comma to the end of the HEX color and only allow letters, numbers, and commas.
Does anyone the regex code for this?

If hex values are always 6 chars long, try this:
^([0-9a-fA-F]{6}(,|$)){4}
See live demo.

Related

Regex for values that are in between spaces

I am new to regex and having difficulty obtaining values that are caught in between spaces.
I am trying to get the values "field 1" "abc/def try" from the sameple data below just using regex
Currently im using (^.{18}\s+) to skip the first 18 characters, but am at at loss of how to do grab values with spaces between.
A1234567890 field 1 abc/def try
02021051812 12 test test 12 pass
3333G132021 no test test cancel
any help/pointers will be appreciated.
If this text has fixed-width columns, you can match and trim the column values knowing the amount of chars between start of string and the column text.
For example, this regex will work for the text you posted:
^(.*?)\s*(?<=.{19})(.*?)\s*(?<=^.{34})(.*?)\s*(?<=^.{46})
See the regex demo.
So, Column 2 starts at Position 19, Column 3 starts at Position 34 and Column 4 (end of string here) is at Position 46.
However, this regex is not that efficient, and it would be really great if the data format is fixed on the provider's side.
Given the not knowing if the data is always the same length I created the following, which will provide you with a group per column you might want to use:
^((\s{0,1}\S{1,})*)(\s{2,})((\s{0,1}\S{1,})*)(\s{2,})((\s{0,1}\S{1,})*)
Regex demo

Regular expression to match a string into a text

I've a TXT file and I use a Reg Expr to get the text after a string
"Diagnosis Statement:"
the Full text is :
Diagnosis Statement:
6/28/2011
RZZZCG77T77G355S
Report text is here ....... end of report
I would get only the 16 digits "RZZZCG77T77G355S" into the text
(16 digits mix of numbers and capital letters)
I can get the text after the string "Diagnosis Statement:" with :
(?ms)^Diagnosis Statement\s*:(?<value>.*)
and I get the code with :
^[A-Z0-9]{16}?$
But cannot get the correct way to merge both and get only the 16 digits string from
text after "Diagnosis Statement:"
Can you give some help ?
On your example, ^([A-Z0-9]{16})$ is enough. Test it here.
Do you have another place in the document where you find text with a format of 16 char, with only captials and digits ? If no, this regex is enough.
With the following regex, you can find the first thing you find with the good format, following "Diagnosis Statement:" and one line of random text.
Diagnosis Statement:\s*.*\s*\K^([A-Z0-9]{16})$
Test it here

Regex to match digits with decimals that have spaces as well as no spaces

I am looking for a regex string to match a set of numbers:
9.50 (numbers without spaces, that have 2 to 4 decimal points)
1 9 . 5 0 (numbers with spaces that have 2 to 4 decimals points)
10 (numbers without spaces and without decimal points)
So far I have come up regex string [0-9\s\.]+, but this not doing what I want. Any cleaner solutions out there?
Many Thanks
Try this:
[\d\s]+(?:\.(?:\s*\d){2,4})?
This makes the decimal point and the digits/spaces after it optional. If there are digits after, it checks that there are 2-4 of them with {2,4}
DEMO
If this should only match the whole string, you can anchor it.
^[\d\s]+(?:\.(?:\s*\d){2,4})?\s*$
The problem with your regex is that it will match 127.0.0.1 as well, which is an IP4 address, not a number.
The following regex should do the trick:
[0-9]+[0-9\s]*(\.(\s*[0-9]){2,4})?
Assumption I've made: You need to place at least one digit (before the comma).
regex101 demo.
(\d+[\d\s]*\.((\s*\d){2,4})?|\d+)
I was still getting "trailing spaces" selected with the third example of 10
This eliminated them.
wouldn't this work as well - '[^. 0-9]' ?
my full postgresql query looks like this:
split_part(regexp_replace(columnyoudoregexon , '[^. 0-9]', '', 'g'), ' ', 1)
and its doing the following:
values in the column get everything except numbers, spaces and point(for decimal) replaced with empty string.
split this new char string with split_part() and call which element in the resulting list you want.
was stuck on this for a while. i hope it helps.

Regex - Limiting the amount of characters in a string

This one is giving me trouble on my assignment.
Assume the alphabet of:
-any lowercase or uppercase letter
-0-9 decimal digits
-_
-$
-%
I want to write an expression that give me strings that:
-starts with a uppercase letter or one of the three symbols
-can only have at most 6 lowercase or uppercase letters
I wanted to try something like
/^[a-z|_|$|%][a-z|A-Z|_|$|%]* {0,3}
but I'm having trouble with keeping track of the "at most" case depending on the initial character
edit: Sorry forgot examples.
_ababab <- OK
ab%$aaaa <- OK
_abababa <- NOT OK, because there is more than 6 alphabet characters
a$ababab <- NOT OK, because there is more than 6 alphabet characters
I think you need to add something like (?=.*[A-Z]{,6})(?=.*[a-z]{,6})
I think you want something like this,
^[A-Za-z](?:[^A-Za-z]*[A-Za-z][^A-Za-z]*){5}$|^[_$%](?:[^A-Za-z]*[A-Za-z][^A-Za-z]*){6}$
DEMO

How to find numbers and exclude any in parentheses using regex

I'm trying to write a regex pattern that will find numbers with two leading 00's in it in a string and replace it with a single 0. The problem is that I want to ignore numbers in parentheses and I can't figure out how to do this.
For example, with the string:
Somewhere 001 (2009)
I want to return:
Somewhere 01 (2009)
I can search by using [00] to find the first 00, and replace with 0 but the problem is that (2009) becomes (209) which I don't want. I thought of just doing a replace on (209) with (2009) but the strings I'm trying to fix could have a valid (209) in it already.
Any help would be appreciated!
Search one non digit (or start of line) followed by two zeros followed by one or more digits.
([^0-9]|^)00[0-9]+
What if the number has three leading zeros? How many zeros do you want it to have after the replacement? If you want to catch all leading zeros and replace them with just one:
([^0-9]|^)00+[0-9]+
Ideally, you'd use negative look behind, but your regex engine may not support it. Here is what I would do in JavaScript:
string.replace(/(^|[^(\d])00+/g,"$10");
That will replace any string of zeros that is not preceded by parenthesis or another digit. Change the character class to [^(\d.] if you're also working with decimal numbers.
?Regex.Replace("Somewhere 001 (2009)", " 00([0-9]+) ", " 0$1 ")
"Somewhere 01 (2009)"