I'm trying to get some exclusions into our sendmail regex for the R command. The following configuration & regex works:
LOCAL_CONFIG
#
Kcheckaddress regex -a#MATCH
[a-zA-Z_0-9.-]+<#[a-zA-Z_0-9-]+?\.+[a-zA-Z_0-9.-]+?\.(us|info|to|br|bid|cn|ru)
LOCAL_RULESETS
SLocal_check_mail
# check address against various regex checks
R$* $: $>Parse0 $>3 $1
R$+ $: $(checkaddress $1 $)
R#MATCH $#error $: "553 Your Domain is Blocked for Unsolicited Mail"
So we are blocking anything#subdomain.domain.us but not anything#domain.us. I'd like to add exclusions for cities and schools so to allow user#ci.somedomain.us and user#subdomain.[state].us. (note that [state] means 1 of the 50 states including DC).
This regex is not working (using CA for California as a test):
(?!.*\#ci\..+?\.us$)(?!.*\#*\..+?\.ca.us$)([a-zA-Z_0-9.-]+#[a-zA-Z_0-9-]+?\.+[a-zA-Z_0-9.-]+?\.(us)$)
I get this error:
sendmail -bt
/etc/mail/sendmail.cf: line 199: pattern-compile-error: Invalid preceding regular expression
What surprises me, in order to get the regex that does work that it requires the leading spaces and I'm not sure what the function of the +<# part of the regex does? What is the less than (<) doing here? Does it need to be added to the bigger regex?
edit: I'm pretty sure that sendmail's R & K commands do not support negative look-aheads. So if anyone can help re-write the regex in a sed-friendly format I'd be grateful!
Your criteria is not clear, you say block all subdomains but then allow them too?
Unless you are using the user name specifically, don't match it.
Block
sub.domain.us
Allow
sub.sub.domain.us
or domain.us
Ksubsubdomains regex -a#MATCH #([a-zA-Z_0-9-]+\.){2}us
Ssubsub
R$+ $: $(subsubdomains $1 $)
R#MATCH $#error $: "553 No Thank You."
# sendmail -bt
Enter <ruleset> <address>
> subsub a#sub.sub.us
subsub input: a # sub . sub . us
subsub returns: $# error $: "553 No Thank You."
> subsub a#sub.sub.sub.us
subsub input: a # sub . sub . sub . us
subsub returns: a # sub . sub . sub . us
since states have 2 letter abbreviations, block sub domains of 3 or more characters
Ksubstates regex -a#MATCH #[a-zA-Z_0-9-]+\.([a-zA-Z_0-9]){3,}+\.us
I ended up taking a different approach as suggested on the SpamAssassin mailing list. I used sendmail's access.db. Since the locality namespaces I want to white list are all fourth-level domain registrations of the form "<organization-name>.<locality>.<state>.us" I simply created 50 entries for all the states like below, starting with rejecting anything.us:
From:us REJECT
From:ma.us OK
From:mi.us OK
I haven't seen ANY false negatives, i.e., missed spam, since enabling this for a few days now.
Related
Given Sample Text -
This is just testing user1 (Dreamer: db-vd) test test user 2
(company1) (Super: ab-cd1): user_lore set from 'Sc' to 'Re', Ext PID
hit from '' to 'tom.hanks1#tt.com', Make O profiles disabled, Make A
Groups enabled, Shift P configurations enabled, A groups set from
'abc' to 'abc'
Pattern to find in the above text -
Ext PID hit from '' to 'tom.hanks1#tt.com'
Need to take care that tom.hanks1 before # is a variable and could be anything like brad_pitt2 or abc.abc123
If the regex pattern is present in the text then I need to do something. What I came up with is given below:
/Ext PID hit from \'\' to \'.*#tt\.com\'/
Doesn't look effective so need help with the regex to be used! Appreciate it.
I am trying to have my regex match the following:
169.254.0.0-169.254.254.255
Could anyone please help how can I achieve this.
so far I have this:
169\.254\.([1-9]{1,2}|[1-9]{1,2}[1-4])
but it would also pick up 169.254.255.1 which should not be one of the matches.
Please help!
thanks
This is the regex I use for general IP validation:
(([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}(?!\d)|2[0-4][0-9](?!\d)|25[0-5](?!\d))[.]?){4}
Breakdown:
1.`[0-9](?!\d)` -> Any Number 0 through 9 (The `(?!\d)` makes sure it only grabs stand alone digits)
2.`|[1-9][0-9](?!\d)` -> Or any number 10-99 (The `(?!\d)` makes sure it only grabs double digit entries)
3.`|1[0-9]{2}` -> Or any number 100-199
4.`|2[0-4][0-9]` -> Or any number 200-249
5.`|25[0-5]` -> Or any number 250-255
6.`[.]?` -> With or without a `.`
7.`{4}` -> Lines 1-6 exactly 4 times
This hasn't failed my yet for IP address validation.
For your specific case, this should do it:
(169\.254\.)((([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}|2[0-4][0-9]|25[0-4])[.])(([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}|2[0-4][0-9]|25[0-5])))
This is very long because I couldn't figure out how to get 169.254.(0-254).255 to check without getting 169.254.255.1 to fail
Edit: Fixed due to comments
the regex ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4]) matches 0-254.
see this page for more discussion
I've written an article that provides regular expressions for all the components of a generic URI (as defined in RFC3986: Uniform Resource Identifier (URI): Generic Syntax)
See: Regular Expression URI Validation
One of the components of a generic URI is an IPv4 address. Here is the free-spacing mode Python version from that article:
re_python_rfc3986_IPv4address = re.compile(r""" ^
# RFC-3986 URI component: IPv4address
(?: (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) \.){3} # (dec-octet "."){3}
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) # dec-octet "."
$ """, re.VERBOSE)
And the un-commented JavaScript version:
var re_js_rfc3986_IPv4address = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
I'm using to develop a regex in order to verify a pattern that will match the following:
abc123
Ab3TF56G
BD356-2
abc123(3x4)
Ab3TF56G(24x37)
BD356-2(105x04)
abc123 (3x4)
Ab3TF56G (24x37)
BD356-2 (105x04)
abc123(3x4x10)
Ab3TF56G(24x37x3)
BD356-2(105x04x14)
abc123 (3x4x10)
Ab3TF56G (24x37x3)
BD356-2 (105x04x14)
I'm admittedly terrible at RegEx, but am following the guide at: www.regexr.com, and have come up with this so far:
([A-Za-z0-9])\((\d[x^)]\d+)\)+
Unfortunately, it stops working when I start trying to account for the possible dash and parathentises.
• The alpha-numeric set can be any length
• That sequence can, but does not require a dash followed by an integer
• Which can also be followed by a open & close parentheses with integers separated by the "x" character (basically dimensions)
Any help would be much appreciated.
EDIT
In addition, the following should fail:
abc123 (3x4x10)shs
sdlk234(3x)
sdlk234(3x0)
sdlk234-2 (3x)333
Ab3T F56G
Try this:
([a-zA-Z0-9-]+)\s?(\([\dx]+\))?
See it working here: https://regex101.com/r/pU9oR4/1
Here is a graphical representation: https://www.debuggex.com/r/uVGo8mrIUYhXHxjP
EDIT
After your shouldn't match examples it turns out a bit more harder, so your new pattern should be:
^([a-zA-Z0-9-]+\b)([\s\d-])?(\((?:(?!0)[\d]+)((x(?:(?!0\b)[\d]+))(x(?:(?!0\b)[\d]+))?)\))?$
edited again
See it working here: https://www.debuggex.com/r/dxPPbPw0mUKQPRWg
I also add the validation so it didn't match:
sdlk234(3x0x0)
sdlk234(3x1x0)
sdlk234(0x1x1)
Following your logic of dimensions
101 Regexp Demo
^[\w-]+\s*(\((?!0\b)\d+(x(?!0\b)\d+)+\))?$
(?!0\b): Negative Lookahead ,make sure that after it can't be 0\b
\b:assert position at a word boundary (^\w|\w$|\W\w|\w\W)
I'm currently debugging a Perl script and encountered some errors which will be described in the following sections below:
In the script, I have this -td variable designed to accept a string like 1n, 5n, 0.3n, 0.8n
But when I try to use the the last two from the said list, the script does not work as intended, and only works fine when I use only the first two from the list.
To give you an overview, I have written some portions of the script, then after the code, I will state my concern:
if (scalar(#ARGV) < 1){ &get_usage() };
# Getoptions Setup ##
GetOptions (
'h|help' => \$help,
'v|version' => \$version,
'i|input=s' => \$input,
'o|output=s' => \$output,
... # more options here
'td=s' => \$td_val, # this is the line in question
'val=s' => \$v_var,
... # more options here
) or die get_usage(); # this will only call usage of script or help
... # more codes here
get_input_arg(); # this function will validate the entries user had inputted
#assigning to a placeholder value for creating a new file
$td_char="\ttd=$td_val" if $td_val;
$td_char=" " if !$td_val;
... # fast forward ...
sub get_input_arg{
...
# here you go, there is wrong in the following regex to accept values such as 0.8n
unless (($td_val=~/^\d+(m|u|n|p)s$/)||($td_val=~/^\d+(m|u|n|p|s)$/)||($td_val=~/^\d+$/)){#
print "\n-td error!\nEnter the required value!\n";
get_usage();
... # more functions here ...
}
For explanation:
On the console user will input -td 5n
The 5n will be assigned to td_val and to td_char and be used for printing later
This 5n will be validated by get_input_arg() function which will pass to the regex unless line.
For 5n input, script work as intended, but when we use -td 0.8n, then after validating it, it will print the error message after the unless line on the console
I know the regex is failing on matching with regards to using 0.8n as td input, but I don't know how can I fix it. Thanks in advance!
You can use
unless (($td_val=~/^\d+(\.\d+)?[munp]s$/)||($td_val=~/^\d+(\.\d+)?[munps]$/)||($td_val=~/^\d+(\.\d+)?$/))
Explanation:
Your regex has \d+ which matches only integers.. so replace it with \d+(\.\d+)? (integral part followed by optional decimal part)
See DEMO
I am trying to have my regex match the following:
169.254.0.0-169.254.254.255
Could anyone please help how can I achieve this.
so far I have this:
169\.254\.([1-9]{1,2}|[1-9]{1,2}[1-4])
but it would also pick up 169.254.255.1 which should not be one of the matches.
Please help!
thanks
This is the regex I use for general IP validation:
(([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}(?!\d)|2[0-4][0-9](?!\d)|25[0-5](?!\d))[.]?){4}
Breakdown:
1.`[0-9](?!\d)` -> Any Number 0 through 9 (The `(?!\d)` makes sure it only grabs stand alone digits)
2.`|[1-9][0-9](?!\d)` -> Or any number 10-99 (The `(?!\d)` makes sure it only grabs double digit entries)
3.`|1[0-9]{2}` -> Or any number 100-199
4.`|2[0-4][0-9]` -> Or any number 200-249
5.`|25[0-5]` -> Or any number 250-255
6.`[.]?` -> With or without a `.`
7.`{4}` -> Lines 1-6 exactly 4 times
This hasn't failed my yet for IP address validation.
For your specific case, this should do it:
(169\.254\.)((([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}|2[0-4][0-9]|25[0-4])[.])(([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}|2[0-4][0-9]|25[0-5])))
This is very long because I couldn't figure out how to get 169.254.(0-254).255 to check without getting 169.254.255.1 to fail
Edit: Fixed due to comments
the regex ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4]) matches 0-254.
see this page for more discussion
I've written an article that provides regular expressions for all the components of a generic URI (as defined in RFC3986: Uniform Resource Identifier (URI): Generic Syntax)
See: Regular Expression URI Validation
One of the components of a generic URI is an IPv4 address. Here is the free-spacing mode Python version from that article:
re_python_rfc3986_IPv4address = re.compile(r""" ^
# RFC-3986 URI component: IPv4address
(?: (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) \.){3} # (dec-octet "."){3}
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) # dec-octet "."
$ """, re.VERBOSE)
And the un-commented JavaScript version:
var re_js_rfc3986_IPv4address = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;