-1

My heart will go on - Celine Dion
My heart will go on - Celine Dion
My heart will go on - Celine Dion
My heart will go on -Celine Dion
My heart will go on- Celine Dion
My heart will go on-Celine Dion

So that is my example String. each of the line must contain a short hyphen. And The space before or after short hyphen can be any number of space even some line have no space before or after short hyphen. I Need to match from every line 2 group of string. My heart will go on and Celine Dion. So Which Regular expression can do this?

/(.*)(\s\-\s)(.*)/gm this regular expression can match when there is a single space before and after the short hyphen ^(.*?)[\p{Zs}\t]+-[\p{Zs}\t]+(.*) This regular expression can match any number of space before and after short hyphen. But problem when there is no space maybe before and maybe after the short hyphen.

2 Answers2

1

The following pattern seems to be working:

(.*?)(?:\s*-\s*)(.*)

This matches:

  • (.*?) all content up to, but not including
  • (?:\s*-\s*) optional whitespace, a hyphen, and optional whitespace
  • (.*) all remaining content

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0
(.*?)\s*-\s*(.*)

Two captures:

  • (.*?) - non-greedy capture of zero or more of any character
  • \s* - zero or more whitespaces
  • - - a literal -
  • \s* - zero or more whitespaces
  • (.*) - capture the rest

Demo

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108