Regex problem

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
palinka
Senior user
Senior user
Posts: 986
Joined: 2017-09-12 17:57

Regex problem

Post by palinka » 2019-05-23 20:40

I have seen a pattern for a type of spam that I've been receiving.

Example (actual received) From: "Lexie" <EdwardWhite@smolka.sk>

The pattern is the name in quotation marks is always a first name and is different from the first name in the email address AND the email address ALWAYS consists of capitalized letters in the first and last name AT whateverdomain.tld.

This is readily apparent to the human eye but if scripted could result in false positives, so at first I just want to test it by just sending myself a message containing the from address every time it hits global rule if From (regex) ^.*((([A-Z][a-z]+)([A-Z][a-z]+))(?=@)).*$

MY PROBLEM is that in testing the regex matching part of the script, the first name of the email address contains the first few letters of the last name.

Pieced together from Soren's scripts...

Code: Select all

Function oLookup(strRegEx, strMatch, bGlobal)
   If strRegEx = "" Then strRegEx = StrReverse(strMatch)
   With CreateObject("VBScript.RegExp")
      .Pattern = strRegEx
      .Global = bGlobal
      .MultiLine = True
      .IgnoreCase = True
      Set oLookup = .Execute(strMatch)
   End With
End Function

' Example:
' "Lexie" <EdwardWhite@smolka.sk>
' (([A-Z][a-z]+)(?=(\"\s\<|\"\<)))  finds Lexie
' (([A-Z][a-z]+)(?=([A-Z][a-z]+\@)))  finds Edward

oMessage_From = """Lexie"" <EdwardWhite@smolka.sk>"

Dim strRegEx, Match, Matches
Dim o_name, o_emailname, sMSG

' find first name outside of <>
strRegEx = "(([A-Z][a-z]+)(?=(\""\s\<|\""\<)))"
Set Matches = oLookup( strRegEx, oMessage_From, False )
For Each Match In Matches
   o_name = Match.Value
Next
wscript.echo o_name

' find first name inside <>
strRegEx = "(([A-Z][a-z]+)(?=([A-Z][a-z]+\@)))"
Set Matches = oLookup( strRegEx, oMessage_From, False )
For Each Match In Matches
   o_emailname = Match.Value
Next
wscript.echo o_emailname

If (o_name = o_emailname) Then
	sMSG = "Hit on name mismatch. Check spam for " & oMessage_From
Else 
	sMSG = "No match"
End If

wscript.echo sMSG
In this example, o_name returns "Lexie" which is correct. However, o_emailname returns "EdwardWhi" instead of "Edward" and because of that, I cannot compare it to "Lexie". I tried other email addresses of varying lengths and it still returns a few letters of the last name. Anyone see what I'm screwing up here? I tried in 2 different regex testers and the regex part is good so I can't figure out what's wrong.

User avatar
SorenR
Senior user
Senior user
Posts: 3169
Joined: 2006-08-21 15:38
Location: Denmark

Re: Regex problem

Post by SorenR » 2019-05-23 20:56

Use https://regex101.com/

Click on the 3 lines top left and choose ECMAScript...

Change .IgnoreCase = True to False :mrgreen:

One thing though... Copy oLookup to a new function or you will break all previous use of the function :!:
SørenR.

The quantum rule of insecurity which states that the act of observing how vulnerable a host or service is changes the insecurity level of the service.

palinka
Senior user
Senior user
Posts: 986
Joined: 2017-09-12 17:57

Re: Regex problem

Post by palinka » 2019-05-23 21:20

Boom! I knew it was something dumb. :mrgreen: Thanks!

And yea, I do use regex101. I like that they have examples and not just worky/no-worky.

Also added function xLookup. :D

Post Reply