3

I Have outgoing emails which go like:

Dear XYZ,

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

CASE ID: 123654

Best Regards,
XYZ

The text could be one or two paragraphs. I want to make two regex. One should give me the text in paragraphs and the other should give me the number that is the CASE ID. The result should look like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.


123654

I have managed to create a RegEx to get the case using (CASE ID\s*[:]+\s*(\w*)\s*)but I haven't been able to extract the paragraph. Any help will be much appreciated.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Azee.
  • 703
  • 1
  • 5
  • 12
  • Try this `(\n|^).*?(?=\n|$)` – Andrii Pryimak Nov 21 '17 at 19:10
  • Or this for only 1st paragraph `(^).*?(?=\n|$)` – Andrii Pryimak Nov 21 '17 at 19:11
  • You first need to find out what is separating the lines, is it only \n or something like \t\r\n or similar – Harry Nov 21 '17 at 19:12
  • @AndriiPryimak : Both of these expressions give me "Dear XYZ," – Azee. Nov 21 '17 at 19:35
  • @Harry : Sorry for the novice question but how may I do that? – Azee. Nov 21 '17 at 19:35
  • That is not so easy to explain, i do this kind of stuff by dumping the message to a file and investigate using hex editor. It might not be neccessary tough. Please define a bit more about your input. E.g. your example shows the 3rd line as what you are looking for, so why do'nt you just parse line by line and assume the 3rd line as paragraph? – Harry Nov 21 '17 at 19:54

1 Answers1

2

Basically you can or should do one regex instead, that will deliver matchgroups.

In almost any other language it would look like this (using "gs" flag to ignore newline):

(.+?)CASE ID: (\d+)

But for vbscript it we have something like this:

(.*?[^\$]*)CASE ID: (\d+)

Also you need to deal with matchgroups like this:

Dim RegEx : Set RegEx = New RegExp
RegEx.Pattern = "(.*?[^\$]*)CASE ID: (\d+)"
RegEx.Global  = True
RegEx.MultiLine = True
Dim strTemp : strTemp = "Lorem ipsum " & VbCrLf & "Cannot be translated to english " & VbCrLf & "CASE ID: 153" 
WScript.Echo RegEx.Execute(strTemp)(0).SubMatches(0)
WScript.Echo RegEx.Execute(strTemp)(0).SubMatches(1)

The thing is that this will only work if the constant string "CASE ID: " is contained in the message. In case the string is missing e.g. the newline after the ":" it would not work

Harry
  • 1,233
  • 10
  • 24
  • I really appreciate the effort you are putting into this, regardless of the result I owe you a cold one :) – Azee. Nov 21 '17 at 20:30
  • Hm i recognize i am too far away from vbscript, could help you in most other languages i guess. You'd need to get out how to make the first part (.+?) match also any newline, so all your input text is parsed as it was one single line. Also vbscript is pretty picky about the array index access in case the arrays dont exist. Did you get my idea and can you deal with it? You can test it on https://regex101.com/ but check "regex options" single line and global... – Harry Nov 21 '17 at 20:49
  • `For Each Item In ActiveExplorer.Selection` 'Search_Email = Item.body' `With RegExp` .`Global = True` .`Pattern = Pattern` .`IgnoreCase = True` `Set Matches = RegExp.Execute(Search_Email)` `End With` `If Matches.Count > 0 Then` `Debug.Print Matches(1)` `ali = Matches(1)` `Else` `Debug.Print "Not Found "` `End If` `Next` `Set RegExp = Nothing` – Azee. Nov 21 '17 at 20:50
  • When I try to print the result it doesnot which means it isnt finding any matches. Yeah vba sucks, I only need it for this assignment and I do not understand the issue here. – Azee. Nov 21 '17 at 20:53
  • Sorted it out now, please try this pattern and add the multiline=true parameter. Despite the result, looking forward to the cold one ;-) – Harry Nov 21 '17 at 21:12