Complex regex: key-value pairs

I cannot find the correct regular expression for this data type:

Phone Work: 1111111111 Phone Fax Work: 222222222 Phone: (333) 333-3333 Email-: email@email.com

Desired Result:

Col1    Col2       Col3
Phone   Work       1111111111 
Phone   Fax Work   222222222 
Phone   General    (333) 333-3333
Email   null       email@email.com

The key consists of two parts (second may be omitted) Phone-Work:,Email-:

Perhaps four types of keys: Phone-, Email-, User ID-,Web address-

I can’t figure out how to create a regular expression that will take a part valueand stop before a new one key.

Here is what I am trying with some data:

https://regex101.com/r/weEc3A/1

+4
source share
2 answers

You can use a solution like

(?si)(Phone|Email|User ID|Web address)-([^:]*):\s*((?:(?!(?:Phone|Email|User ID|Web address)-).)*)

(?si)(Phone|Email|User ID|Web address)-([^:]*):\s*(.*?)(?=(?:Phone|Email|User ID|Web address)-|$)

regex

  • (Phone|Email|User ID|Web address)- - , -, 1
  • ([^:]*) - , :, 2
  • :\s* -
  • ((?:(?!(?:Phone|Email|User ID|Web address)-).)*) - 3, char (.), , (?:Phone|Email|User ID|Web address)-.

(?s), . . # RegexOptions.Singleline . (?i) RegexOptions.IgnoreCase. : (?si).

+4

, :

(.*?)-(.*?):\s*([- ()+\w@.]+)(?![-\w]*:)
+1

Source: https://habr.com/ru/post/1661582/


All Articles