How to spilt string?

How do I spilt the following string? I was thinking to use regex but can’t get my head around it.

Example 1
Part of: Asia Perspectives: History, Society, and Culture (15 Books) | by John Nathan | May 15, 2018
What I want is “John Nathan” and “May 15, 2018” in two separate strings.

Example 2
by Rebecca E. Karl | Aug 13, 2010
What I want is “Rebecca E. Karl” and “Aug 13, 2010” in two separate strings.

is the keyword “By” common ?

If so you can refer this regex

1 Like

Use this below code @AnJoe_Ang
Split(Split(“History, Society, and Culture (15 Books) | by John Nathan | May 15, 2018”,“by”)(1).Trim,"|")(0) - For “John Nathan”
Split(Split(“History, Society, and Culture (15 Books) | by John Nathan | May 15, 2018”,“by”)(1).Trim,"|")(1) - For “May 15, 2018”
And
Split(Split(“by Rebecca E. Karl | Aug 13, 2010”,“by”)(1).Trim,"|")(0) - For “Rebecca E. Karl”
Split(Split(“by Rebecca E. Karl | Aug 13, 2010”,“by”)(1).Trim,"|")(1) - For “Aug 13, 2010”

Hope this may help you :slight_smile:

1 Like

Hi,

Can you try the following expression?

mc = System.Text.RegularExpressions.Regex.Match(text,"(?<=(^|\|\s+)by\s+)([^\|]+)\|([^\|]+$)")

note: mc is Match type variable.

name : mc.Groups(2).Value
date : mc.Groups(3).Value

Regards,

Assuming every time at least “by (some name string) | (Date)” present at end of string
I think you can split string based on “|” and you will get some string array. Then you will have to get last 2 element from array. As there can be some times 2 or 3 element present in array based on above example. So getting last 2 will work here.

Thanks! I combine your solution with @hasib08’s regex and change the following for my use.
name : mc.Groups(1).Value
date : mc.Groups(2).Value

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.