Globally replace obsolete NLog.WarnException using regular expression

I’ve updated NLog ver 2.0 to 4.3.5. After this I’ve noticed a compiler warning that 
WarnException(msg,exc) is obsolete and should be replaced by Warn(exc,msg)

It could be globally replaced using regular expressions 

WarnException\((.*),(.*)\);
to 

Warn($2, $1);

See saved regex in https://regex101.com/r/kL8cR4/1

#nlog, #regex

FromToRange generic class

Quite often you need to return from method 2 values that describe the range, e.g. From/To or Min/Max. You could use standard MS classes Pair or Tuple https://msdn.microsoft.com/en-us/library/system.tuple.aspx. However the names of properties are not describe the meaning of the values.
So I am using own class FromToRange. It is done similar to Pair(from http://blog.o-x-t.com/2007/07/16/generic-pair-net-class/)

///<summary>
/// similar to Pair(from http://blog.o-x-t.com/2007/07/16/generic-pair-net-class/"
///See also  DateTimeRange ( https://github.com/MNF/CommonDotNetHelpers/blob/master/src/Dates/DateTimeRange.cs)
///</summary>

public class FromToRange
{
public T From { get; set; }
public T To { get; set; }

public FromToRange()
{
}

public FromToRange(T from, T to)
{
this.From = from;
this.To = to;
}
public override string ToString()
{
string sRet = String.Format("From {0} to {1}", From, To);
return sRet;
}
public override bool Equals(object obj)
{
if (this == obj) return true;
FromToRange pair = obj as FromToRange;
if (pair == null) return false;
return Equals(From, pair.From) && Equals(To, pair.To);
}

public override int GetHashCode()
{
return (From != null ? From.GetHashCode() : 0) + 29 * (To != null ? To.GetHashCode() : 0);
}

}

Changes in .psproj do not cause re-build

With PostSharp I  am using .psproj files 
If I did changes in .psproj file,  simple build didn’t recognize that the project has been changed and skipped the project.

 I had to manually Rebuild the project.

The fix is to change build type of the .psproj file from none to ‘content’ ( may be other types also will work)

#build, #postsharp

GMail considered “unsubscribe” at the end of email as spam

I’ve created a new email account @gmail.com to communicate with members of Russian Bards Songs Club . When I sent the first email to 15 people, it was rejected with the message:

Delivery to the following recipient failed permanently:

Technical details of permanent failure:

Message rejected.  See https://support.google.com/mail/answer/69585 for more information.

I’ve sent the same email to a single person and it was successful. I’ve wrote a small program to  vary some details and number of addressees, but the emails were continue to be blocked. I wasn’t able to send the email even to a single recipient. It means that gmail has more strict anti-spam criteria for smtp  clients then for gmail senders.

The email I was trying to send was written in Russian with some words in English and contained a few links. I didn’t know, what else make my email suspicious and submitted a question to Google. But they normally do not respond.

I’ve decided to test smaller emails to identify if particular part of content flags the email as spam. After a few attempts I found, that the trouble caused  the phrase

If you don’t want to receive more emails from us, please reply with the word ‘Unsubscribe’ in the subject.

I’ve seen multiple recommendations to include unsubscribe information in emails to bypass anti-spam filters. It seems that too many spammers started to add such sentences, that Google now consider it as a spam indication.

Maybe  they didn’t like that I have an instruction to unsubscribe via email instead of providing link to website.

For now, I removed the sentence from the email and finally was able to send it.

Update:

Couple weeks later I added a message “If you don’t want to receive emails any more, click on this link”( in Russian), but the actual URL contained the word “unsubscribe”. I’ve sent emails to 3 big groups of people(about 100 each). Two of them were sent successfully, but one was rejected(actually the first one).
I split the failed group in three smaller ones, and tried to send them. They were rejected again.

Additional annoyance was that “delivery failed” notifications were received slowly within 5 minutes, and initially I had an impression that some emails succeeded, and only some were rejected. However according to timestamp all emails were rejected at the same time.

Then I renamed the target URL to not use “unsubscribe” word, updated the link in email signature, and was able to send emails successfully.

RegEx for credit-card-numbers with spaces/dash delimiters

I’ve used regular expressions from http://www.richardsramblings.com/regex/credit-card-numbers/ that supports delimiters spaces/dashes, e.g. “3714-496353-98431” or “3714 496353 98431”. (by the way I’ve added Diner’s Club pattern  ((?:30[0-59]|(?:(?:36|38|39)[0-9]))[0-9]{11}) even they are obsolete now)
Unfortunately they use backreferences \1 in individual credit card matches, which doesn’t work if I use alternatives (Visa)|(Master)|(Amex).
I replaced numbered capturing groups with named capturing groups http://www.regular-expressions.info/named.html .
Javascript and .Net format for named groups are different. I’ve done initially using ?P syntax
(?<!\d)(?:(4\d{3}((?P[\ \-]?)(?:\d{4}(?P=DelimVI)){2}\d(?:\d{3})?)|(5([1-5]\d{2})(?P[\ \-]?)\d{4}(?P=DelimMC)\d{4}(?P=DelimMC)\d{4})|(3[47]\d\d(?P[\ \-]?)\d{6}(?P=DelimAX))\d{5}))(?!\d)
and then had to translate to .Net syntax
(?<!\d)(?:(4\d{3}(?’DelimVI'[\ \-]?)(?:\d{4}\k’DelimVI’){2}\d(?:\d{3})?)|(5([1-5]\d{2})(?’DelimMC'[\ \-]?)\d{4}\k’DelimMC’\d{4}\k’DelimMC’\d{4})|(3[47]\d\d(?’DelimAX’)[\ \-]?)\d{6}\k’DelimAX’\d{5}))(?!\d)
I’ve tested Javascript syntax in RegEx101 (see https://regex101.com/r/dX5zJ4/3), but for .Net I had to use http://regexstorm.net/tester and they are not comparable-RegEx101 is significantly user friendlier.

#credit-cards, #regex, #regular-expressions

Use negative look-arounds regex to ensure pattern not surrounded by undesired characters.

I need to extract from text possible credit card numbers. I have regex for different types of credit cards from http://www.richardsramblings.com/regex/credit-card-numbers/. But if the match preceded or followed by any digit, it is not considered as a valid credit card number, but just as unrelated number.
I’ve tried to add non-capturing group (?:\D|) before and after expected card regex,e.g
(?:\D|^)(?:(?:(?:(4[0-9]{12}(?:[0-9]{3})?))|(3[47][0-9]{13}))(?:\D|$))
It mostly works OK, but if 2 credit cards are next to each other, separated by single character:
 4740515144050,378282246310005
the first card matched, but the second is not found.
Negative lookahead is indispensable if you want to match something not followed by something else.
So I was able to search what I need using negative look-arounds: negative lookbehind (?<!\d) before the expression and negative lookahead (?!\d) after the expression
(?<!\d)(?:(?:(?:(4[0-9]{12}(?:[0-9]{3})?))|(3[47][0-9]{13})|(?:5[1-5][0-9]{14}))(?!\d))
See my example of regex in https://regex101.com/r/iY2aR6/8

 

I want to clarify that the purpose of non-capturing groups is different to look-arounds .
In regex, normal parentheses not only group parts of a pattern, they also capture the sub-match to a capture group.  At other times, you do not need the overhead.
If you need parentheses to work in natural way to only group parts of a pattern, you need to specify non-capturing group by putting ?: after opening parenthesis

#lookahead, #lookbehind, #non-capturing-group

Is it possible to determine Visual Studio edition during c# compile time

To use MS Fakes shims, I had to conditionally exclude related tests  to allow easily bypass them on Community and Professional Editions of the Visual Studio.
#if !FAKES_NOT_SUPPORTED
[ TestMethod()]
Fakes related code
#endif
I tried to find does edition automatically pre-defined by compiler, but it’s not included in Conditional Compilation Constants

Furthermore,  these constants seems defined in VB only, and not in c# and not updated since VS2008.

The reasons are discussed in “Pre-defined Constants, preprocessor directives, etc… “

The “C# Language Designers” decided to not include constants in the fashion in which C/C++ allows them.  To allow some, or even one, would open a Pandora’s Box of voices suggesting new constants to be added.

But I disagree,  because if the limitation coming from VS , it should provide a way to  determine it in pre-condition.
The related article shows how to check installed Visual Studio on run-time
Unfortunately I didn’t find, is it possible to determine Visual Studio edition  during c# compile time?

#compile-time, #constants, #visual-studio