roccoo
roccoo

Reputation: 33

Regular Expression match this string

i have a this strings

01 - Il visitatore - Lia and me II

i write this regex

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

but match only number. as a result I would

Group1: 01 
Group2: Il visitatore misterioso

Upvotes: 0

Views: 31

Answers (2)

Tân
Tân

Reputation: 1

Hope this helps!

var string = '01 - Il visitatore misterioso - Mia and me II';
var regex = /^(\d{2})\s\-\s([\w\s]+)\-[\w\s]+$/;
var matches = regex.exec(string);

console.log('Group 1:', matches[1])
console.log('Group 2:', matches[2])

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627609

If you have access to a programming code, you could easily split the string with "space"-"hyphen"-"space", and get the necessary items.

With a regex, you may use

^(\d+)\s*-\s*([^-]+)\s

See the regex demo

Details:

  • ^ - start of string
  • (\d+) - Group 1 capturing 1 or more digits
  • \s*-\s* - a hyphen enclosed with 0+ whitespaces
  • ([^-]+) - Group 2 capturing 1+ chars other than -, as many as possible (the + quantifier is greedy)
  • \s - a whitespace

Upvotes: 1

Related Questions