Activity › Forums › Adobe After Effects Expressions › Create Null Catch for Count Number of Characters in String
-
Create Null Catch for Count Number of Characters in String
Posted by Bryce Poole on August 15, 2014 at 8:02 pmHello,
I have an expression that is meant to count the number of times particular characters appear in a string, the expression works fine as long as that character actually appears in the string. However, when the character doesn’t appear it just defaults to counting string length. Since the string is user enterable I’d like to create a catch for when none of the characters are present that just returns 0, but I’m not quite sure how to write this.
This is what I have right now. Could anyone help me out with writing this?
Thanks!
//defines the variablestxt = thisComp.layer("TXT Here").text.sourceText;
a = (txt.match(/e|f/g)||[]).length//print total value
a;
Bryce Poole replied 11 years, 11 months ago 2 Members · 9 Replies -
9 Replies
-
Bryce Poole
August 15, 2014 at 8:31 pmI may have answered my own question, putting what I did here incase anyone else has a similar question in the future.
I used indexOf to check for the desired input characters then used an if/else statement to either count the number of desired characters or set the variable to 0.
This appears to be working, if anyone else has a more elegant solution let me know.
Thanks!
//defines the variables
txt = thisComp.layer("Overlay TXT Here").text.sourceText;//statement body
if (txt.indexOf("e"|"f"))
a = (txt.match(/e|f/g)||[]).length
else
a=0;//print total value
a -
Declan Smith
August 15, 2014 at 8:40 pmHi
I entered your expression onto one text layer and had text on a second layer and it worked as expected. i.e. it counts the number of occurrences of e and fIf e and f do not appear in the text layer it registers 0, so I can’t see the broken condition. I’m using AE CS6
//defines the variables
txt = thisComp.layer("TXT TEST").text.sourceText;
a = (txt.match(/e|f/g)||[]).length
//print total value
a;Declan Smith
https://www.madpanic.tv
After Effects CS6/ FCS3 / Canon XLH1 / Canon 7D / Reason / Cubase“it’s either binary or it’s not”
-
Bryce Poole
August 15, 2014 at 9:18 pmHey Declan,
Your right that one does seem to be okay on its own, specifically I’m running into it when I’m trying to count punctuation like in the code below, that seems to be returning total string length. I’m guessing its because AE is seeing the “.” and “!” as part of the expression as opposed to string values?
Any thoughts on how to address this?
b = ((firstline.match(/.|!/g)||[]).length) -
Declan Smith
August 15, 2014 at 9:28 pmAh! I see now.
Ok the reason it’s returning the string length is that the matching characters are regular expressions. The ‘.’ is a special character that means any character. You have to escape it by putting a backslash before it.
b = ((firstline.match(/\.|!/g)||[]).length)Declan Smith
https://www.madpanic.tv
After Effects CS6/ FCS3 / Canon XLH1 / Canon 7D / Reason / Cubase“it’s either binary or it’s not”
-
Declan Smith
August 15, 2014 at 9:35 pmGlad it helped. There are a bunch of other characters that will also need escaping, such as
/ * + ? | ( ) [ ] { }
Also if you want to count the backslash itself, then escape that by putting another backslash in front i.e. \\
Declan Smith
https://www.madpanic.tv
After Effects CS6/ FCS3 / Canon XLH1 / Canon 7D / Reason / Cubase“it’s either binary or it’s not”
-
Bryce Poole
August 15, 2014 at 10:29 pmHey Declan,
Okay cool, thank you! I seem to be having issues with counting \ and “, though both are escaped out.
Entering a \ is causing after effects to disable the expression and give an “Unterminated string constant” error and ” just isn’t counting, any thoughts?
Thanks again!
var i = (firstline.match(/f|t|\/|\\|\"/g)||[]).length; -
Declan Smith
August 16, 2014 at 2:02 pmHi Bryce,
Yes, some more weirdness of javascript etc I’m afraid. The first issue, i.e. \ needs an addition \n at the end of the line otherwise the regular expression sees it as an unterminated line. The next issue is with speech marks in that the UI interface seems to insert opening or closing speech marks which don’t match with what is put in the expression (by the same UI), so you need to specify the exact unicode characters you want to match. Please see below.
i.e. Opening speech marks is 8220 (\u201C) and closing speech marks is 8221 (\u201D).
If you get any other weird characters, then just use:
txt.charCodeAt(0)
to print the decimal version of the character, then convert that to Hexidecimal and then add it to a unicode match. The above assumes 1 character at position 0.
Hope this helps
var txt = thisComp.layer("TXT").text.sourceText + "\n";
var i = (txt.match(/\\|T|\u201C|\u201D|,|\//g)||[]).length;
i;Declan Smith
https://www.madpanic.tv
After Effects CS6/ FCS3 / Canon XLH1 / Canon 7D / Reason / Cubase“it’s either binary or it’s not”
Reply to this Discussion! Login or Sign Up