INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Regex help sought  (Read 1664 times)

Mike48

  • World Citizen
  • ***
  • Posts: 214
Regex help sought
« on: November 16, 2012, 10:14:17 pm »

My library includes many tracks with titles like

Trio Sonata for organ No. 3 in D minor, BWV 527 (BC J3): Adagio e dolce

from which I would like to remove (BC J3) and the preceding space.

The material after "BC" differs from track to track, but there is always a closing ")".

My impression is that the only way to use wildcards in MC expressions is with regular expressions and the regex() function.  So I wrote this:

=replace([name],Regex([name],/ /(BC.+/),1))

which I thought would remove that bit, but instead it removes everything after the "(".

I am baffled. Help will be appreciated, to say the least!
Logged

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Regex help sought
« Reply #1 on: November 17, 2012, 02:11:57 am »

Try this:

   =if(Regex([Name], /#^(.*) \(BC[^)]*\)(.*)#/),[R1][R2],[Name])
Logged
The opinions I express represent my own folly.

Mike48

  • World Citizen
  • ***
  • Posts: 214
Re: Regex help sought
« Reply #2 on: November 27, 2012, 08:48:24 pm »

Try this:

   =if(Regex([Name], /#^(.*) \(BC[^)]*\)(.*)#/),[R1][R2],[Name])

Thank you VERY much!  It works beautifully.

After much study, I think I've got it . . .

  • The first ^ anchors the match at the start of the line
  • Then (.*) is any number of anything; the () assigns the match a number
  • Then the open paren is an escaped literal; [^)]* is any number of anything BUT a closing paren
  • Again, (.*) is any number of anything; assigned a number

A concise language, but not an easy one.
Logged

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Regex help sought
« Reply #3 on: November 27, 2012, 09:08:15 pm »

Great!

The  [^)] is a negated character class, btw.  The character class [] means match any character within the square brackets, and a ^ as the first character negates this to mean match any single character not listed in the brackets.
Logged
The opinions I express represent my own folly.

Mike48

  • World Citizen
  • ***
  • Posts: 214
Re: Regex help sought
« Reply #4 on: November 27, 2012, 09:21:48 pm »

So, did you use regular expressions first in another context (programming), or did you learn them within MC?
Logged

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Regex help sought
« Reply #5 on: November 27, 2012, 09:31:38 pm »

I'm dating myself, but Jimmy Carter was still the US president.  'nuff said?
Logged
The opinions I express represent my own folly.

Mike48

  • World Citizen
  • ***
  • Posts: 214
Re: Regex help sought
« Reply #6 on: December 01, 2012, 01:06:45 am »

I'm dating myself, but Jimmy Carter was still the US president.  'nuff said?

Carter?  A recent president.

My first programming course was in Lyndon Johnson's time -- so I may have more white hair than you.

Logged
Pages: [1]   Go Up