user2831003
user2831003

Reputation: 53

Regex all the contents Using regex

I am new to regular expression. I got the following pattern,

LastName, FirstName 04/12/2014

AAAA

BBBB

CCCC

LastName, FirstName 04/12/2014

DDDD

EEEE

FFFF

LastName, FirstName 04/12/2014

GGGG

HHHH

I want to split the above string by LastName, FirstName 04/12/2014 and its contents. So, for the above example, I will get 3 Strings.

String 1 :

LastName, FirstName 04/12/2014

AAAA

BBBB

CCCC

String 2 :

LastName, FirstName 04/12/2014

DDDD

EEEE

FFFF

String 3 :

LastName, FirstName 04/12/2014

GGGG

HHHH

I am using the following regular expression to match the pattern,

But it is not getting the contents,

[a-zA-Z]*\s*\,\s*[a-zA-Z]*\s*\d{2}\/\d{2}\/\d{4}

Please let me know how do I split the string like above using regex.

Upvotes: 3

Views: 62

Answers (2)

Krishna M
Krishna M

Reputation: 1195

Use this regex

LastName(.*?)(?=LastName|$)

RegexDemo

Upvotes: 0

Trygve Flathen
Trygve Flathen

Reputation: 696

  1. Make sure you use a multiline regex. How to do this depend on the language, e.g. Appending "m" after the regex or using a multiline flag when creating the regex.

  2. Your regex does not contain code for anything after the date. How to code this depends on the nature of your "contents". Make sure the code can not match the next LastName,...-line.

Upvotes: 1

Related Questions