INTERACT FORUM

Please login or register.

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

Author Topic: [SOLVED] Find hanging (i.e. not closed) brackets in tags  (Read 811 times)

afora

  • Recent member
  • *
  • Posts: 29
[SOLVED] Find hanging (i.e. not closed) brackets in tags
« on: October 05, 2022, 09:44:59 pm »

Hi guys, anyone could be so kind to share an approach (perhaps, an expression?) to find all tracks where the closing part of a bracket pair is missing?

I have quite a few tags which were truncated at some stage, and I would like to fix them.

Many thanks for your help!
Logged

lepa

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2033
Re: Find hanging (i.e. not closed) brackets in tags
« Reply #1 on: October 06, 2022, 06:40:37 am »

Something like this. Example is for name field and prints UNMATCH if "[" and "]" count doesn't match
Code: [Select]
If(Compare(ListCount([Name]A,/[), =, ListCount([Name]A,/])),,UNMATCH)
E: edited expression to have [Name]A as a source. "A" artificial postfix is there to so we get correct letter count ("[" or "]" in this example) from ListCount function return. You could use any letter as a postfix other than the letter (delimiter) you want to count
Logged

blgentry

  • Regular Member
  • Citizen of the Universe
  • *****
  • Posts: 8014
Re: Find hanging (i.e. not closed) brackets in tags
« Reply #2 on: October 06, 2022, 07:15:40 pm »

@lepa:  I have no idea how that "untrim" and "postfix" works in your expression.  Unless you are just using those words to add character count or something?

Everyone knows I always reach for REGEX with this kind of task.  But it's not very elegant to try to do in pure regex.  So I used two very simple regex that detect "[" and "]" and then XORed the results together.  Which means that the expression matches if it finds one or the other, but not both.

My version:

Code: [Select]
if(math(regex([Name],/#\[#/,0,0) - regex([Name],/#\]#/,0,0) ),UNMATCHED,)
It's not any better than lepa's version.  Just different.  Easier for me to understand.

Brian.
Logged

blgentry

  • Regular Member
  • Citizen of the Universe
  • *****
  • Posts: 8014
Re: Find hanging (i.e. not closed) brackets in tags
« Reply #3 on: October 06, 2022, 07:24:39 pm »

...and because sometimes I can't help myself, here's a version that detects unmatched [ ] or ( ) .

Code: [Select]
if(math(math(regex([Name],/#\[#/,0,0) - regex([Name],/#\]#/,0,0) ) | math(regex([Name],/#\(#/,0,0) - regex([Name],/#\)#/,0,0))),unmatched,)
I could add in { } and/or <> also, but does anyone use those for delimiters?

Brian.
Logged

lepa

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2033
Re: Find hanging (i.e. not closed) brackets in tags
« Reply #4 on: October 07, 2022, 12:24:03 pm »

@lepa:  I have no idea how that "untrim" and "postfix" works in your expression.  Unless you are just using those words to add character count or something?
it is just something to manipulate MC to give correct listcount return in case of [ or ] is the last character. It can be anything. To optimize parsing speed just one letter can be used there.

E: Doesn't that regex just return 1 for any amount [ or ]?

So "All there is [ ] ]" will not be marked as unmatched
Logged

blgentry

  • Regular Member
  • Citizen of the Universe
  • *****
  • Posts: 8014
Re: Find hanging (i.e. not closed) brackets in tags
« Reply #5 on: October 07, 2022, 05:52:11 pm »

it is just something to manipulate MC to give correct listcount return in case of [ or ] is the last character. It can be anything. To optimize parsing speed just one letter can be used there.

Thank you for explaining!  It was very confusing for me, as your choice of "words" seemed like MC keywords. But they were not!  Nice.  :)

Quote
E: Doesn't that regex just return 1 for any amount [ or ]?

So "All there is [ ] ]" will not be marked as unmatched

Well I hadn't thought of that.  You are definitely correct.  Your way is far more exact.  Mine would not detect any number of scenarios that had one set of matched brackets, but one or more hanging brackets...

Brian.
Logged

blgentry

  • Regular Member
  • Citizen of the Universe
  • *****
  • Posts: 8014
Re: [SOLVED] Find hanging (i.e. not closed) brackets in tags
« Reply #6 on: October 07, 2022, 06:08:33 pm »

I couldn't stand it and had to fix my expression to make it handle any number of unmatched brackets.  But this solution is essentially just lepa's solution using a tiny difference in syntax by explicitly looking for the character using regex.  It's kinda pointless. 

Lepa should really get the credit here; not me.

But I'm sharing anyway.

Code: [Select]
if(math(listcount(regex([Name],/#(\[)#/,-2,0)) -  listcount(regex([Name],/#(\])#/,-2,0))),UNMATCH,)
Brian.
Logged

lepa

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2033
Re: [SOLVED] Find hanging (i.e. not closed) brackets in tags
« Reply #7 on: October 07, 2022, 06:13:18 pm »

Regex is always nice. Wish it would come natural for me also.If only one pair of [] is expected then the regex looks cleaner solution for sure.

Edited my take on this to have an explanation as it is kind of cheating way to get count for certain letter using list function.

Nice second take Brian! That's certainly the best as it doesn't use any hacks

E: This could be useful also as simple letter counter. Counting e.g. "A"s:
ListCount(Regex([Name],/#(A)#/,-2,0))
Logged
Pages: [1]   Go Up