INTERACT FORUM

Please login or register.

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

Author Topic: Regex assistance needed - discard and replace in a single pass?  (Read 1479 times)

marko

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 9139

Let's say I have a string like so:

!Birthdays\June's Birthday

I want to discard the exclamation mark, and replace the back slash with ": " (colon+space without the quotes).

I figured that if this was possible, it would have to be via regex, but have spent too much time on it now without success, I have to stop. I may be trying to do something that's not possible.

I was working on the theory that all I needed to do was 'capture' everything after the exclamation mark, and then do something along the lines of replace([R1],\,: ) but I am failing, miserably :)
Any ideas out there?

blgentry

  • Regular Member
  • Citizen of the Universe
  • *****
  • Posts: 8014
Re: Regex assistance needed - discard and replace in a single pass?
« Reply #1 on: July 17, 2015, 09:53:24 am »

Just tried your example as an expression column and then as a replacement using the "=" assignment in the Tagging Pane.  This regex seems to do exactly what you are asking for:

Code: [Select]
regex([Name],/#!(.+)\\(.+)#/,-1,0)[R1]:[R2]
I'm using the [Name] field as my source, and you can substitute whatever field you want.  I'm sure you know that, but I'm including it for completeness.

Let me know if it works or not and I can try to tweak or modify it to work as you are expecting.

Brian.
Logged

mattkhan

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 4226
Re: Regex assistance needed - discard and replace in a single pass?
« Reply #2 on: July 17, 2015, 09:53:55 am »

in regex alone, one way is;

search: (!)([a-zA-Z]+)(\\)(.*)
replace: $2:$4

so 4 capturing groups, discard the 1st one (the !) and the 3rd one (the \) but use the 2nd one (Birthdays) then a literal : and then the 4th one (June's Birthday)

I don't know the jriver syntax v well but perhaps that gives you some ideas
Logged

marko

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 9139
Re: Regex assistance needed - discard and replace in a single pass?
« Reply #3 on: July 17, 2015, 10:46:43 am »

Brian, that's brilliant, thank you. It's for thumbnail text and image playback captions. The field data is nested keywords and I use the exclamation mark to ensure that the "folders" are listed first. It works well enough in a pane, but was not very friendly when used elsewhere.

The goal: If there's a caption, use that, if not, if there's an occasion, use that, if not, if there's a location, use that, otherwise, blank. I have that now, along with formatting that makes things a bit more viewer friendly...

Code: [Select]
ifelse(!isempty([caption]),[caption],!isempty([occasion]),if(isequal([occasion],!,8),regex([Occasion],/#!(.+)\\(.+)#/,-1,0)if(isequal([R1],\!,8),replace([R1],\!,/,/ ),[R1]): [R2],[occasion]),!isempty([location]),if(isequal([location],!,8),regex([location],/#!(.+)\\(.+)#/,-1,0)if(isequal([R1],\!,8),replace([R1],\!,/,/ ),[R1]): [R2],[location]),1,[caption])


For the few cases where the tree has gone two levels deep, I have worked a couple of replace expressions on [R1], and the job's a good 'un.

Brian, and mattkhan, thank you for replying. I'm reasonably fluent with MC expressions and when I see one, I can just read it and know what it's supposed to do. Try as I might, I just cannot seem to be able to get my head around regex. Loving these image captions now :D

-marko

blgentry

  • Regular Member
  • Citizen of the Universe
  • *****
  • Posts: 8014
Re: Regex assistance needed - discard and replace in a single pass?
« Reply #4 on: July 17, 2015, 03:29:28 pm »

Wow, that's a complicated expression.  I'm happy my regex helped get you what you wanted.  :)

brian.
Logged
Pages: [1]   Go Up