extracting last letters from a random letter length source text by substring

IMG_20210809_213756.jpg
Hello, I would like to get the last word let's say last 4 characters of the source text that has a random characters length. I tried to use the negative sign but it says Java lang error and something, so what indexes should I use to get the last characters that I need?
 

Dm114

Well-known member
View attachment 807
Hello, I would like to get the last word let's say last 4 characters of the source text that has a random characters length. I tried to use the negative sign but it says Java lang error and something, so what indexes should I use to get the last characters that I need?
Use magic text [strlen=...] applied to your source string to get its length. Then, as start index use '4 - this length' and as end index use 'this length'.
 
Use magic text [strlen=...] applied to your source string to get its length. Then, as start index use '4 - this length' and as end index use 'this length'.
I really don't know where to find this magic text I tried to use it as [strlen=[not_title]] and add the index 4,0 I also store it to a var but the var always says '[str'
I'm doing it wrong so can you show a Pic for example, much appreciated
 

420

Active member
I really don't know where to find this magic text I tried to use it as [strlen=[not_title]] and add the index 4,0 I also store it to a var but the var always says '[str'
I'm doing it wrong so can you show a Pic for example, much appreciated
you should write the [not_title] to a string variable, then you can use text manipulation - extract text action to capture the last word.

"source text" should be your string variable and "text to match (regex)" should be this "\b(\w+)$" (without the quotation marks)

you can save it to a new string variable, so you can still acces the full [not_title] text in the other variable.

edit: you might actually be able to extract the last word from the notification title directly, if you put [not_title] as source text, i never tried this.
 
Last edited:
you should write the [not_title] to a string variable, then you can use text manipulation - extract text action to capture the last word.

"source text" should be your string variable and "text to match (regex)" should be this "\b(\w+)$" (without the quotation marks)

you can save it to a new string variable, so you can still acces the full [not_title] text in the other variable.

edit: you might actually be able to extract the last word from the notification title directly, if you put [not_title] as source text, i never tried this.
woah it extract the characters in the [not_title] but how can I control how many char should it extract? right now it only extract last 3 letters, also thanks in advance.
 

Dm114

Well-known member
you should write the [not_title] to a string variable, then you can use text manipulation - extract text action to capture the last word.

"source text" should be your string variable and "text to match (regex)" should be this "\b(\w+)$" (without the quotation marks)

you can save it to a new string variable, so you can still acces the full [not_title] text in the other variable.

edit: you might actually be able to extract the last word from the notification title directly, if you put [not_title] as source text, i never tried this.
Why do you use 'Regex text to match'? It's useless as he just want to get the N last characters of a string. If the source text is the notification one, it's even simpler...
 
Why do you use 'Regex text to match'? It's useless as he just want to get the N last characters of a string. If the source text is the notification one, it's even simpler...
it works but yeah it's hard to understand so I also need to know how it works
 

420

Active member
woah it extract the characters in the [not_title] but how can I control how many char should it extract? right now it only extract last 3 letters, also thanks in advance.
you can't control it, this just captures the entire last word in the string. if you only want a certain amount of characters you should follow Dm114's method.
 

420

Active member
Why do you use 'Regex text to match'? It's useless as he just want to get the N last characters of a string. If the source text is the notification one, it's even simpler...
this was one of the things he asked in his first post, if he wants only a certain amount of characters he should follow your method.
 

Dm114

Well-known member
woah it extract the characters in the [not_title] but how can I control how many char should it extract? right now it only extract last 3 letters, also thanks in advance.
This not the right way to achieve what you need (unless I misunderstood).
1⁰) if you need to extract info from notifications, use the 'Notification' trigger to get relevant magic texts
2⁰) store [not_title] or [notification] into a string variable, let's say 'str'
3⁰) store [strlen=str]-4 (or whatever number of characters you need) into an integer variable, let's say 'int'
4⁰) use substring action with 'str' as source text, 'int' as start index and [strlen=str] as end index

You can store the result either in the same 'str' variable if you don't need it anymore or in another one
 
This not the right way to achieve what you need (unless I misunderstood).
1⁰) if you need to extract info from notifications, use the 'Notification' trigger to get relevant magic texts
2⁰) store [not_title] or [notification] into a string variable, let's say 'str'
3⁰) store [strlen=str]-4 (or whatever number of characters you need) into an integer variable, let's say 'int'
4⁰) use substring action with 'str' as source text, 'int' as start index and [strlen=str] as end index

You can store the result either in the same 'str' variable if you don't need it anymore or in another one
where do I store [strlen=str]-4 if the integer only accepts number? there's an expression but it doesn't also accept it.
 

Dm114

Well-known member
where do I store [strlen=str]-4 if the integer only accepts number? there's an expression but it doesn't also accept it.
After pressing button 'Expression', either type it in as indicated ([strlen=str]) or use magic texts (using the [...] submenus).
 

tenduramax2

New member
.{3}$

The above regex will capture the last 3 characters from a end of line. Replace the number with whatever number you want depending on how far back you want to go.Screenshot_20210820-112222_RegexH.png
 

Dm114

Well-known member
.{3}$

The above regex will capture the last 3 characters from a end of line. Replace the number with whatever number you want depending on how far back you want to go.View attachment 863
Correct. But only before an end of line (not a separator, for instance). As '.' stands for 'any kind of character' it could be useful to limitate to a set of character such as [0-9] (i.e. \d) \w
 

tenduramax2

New member
Correct. But only before an end of line (not a separator, for instance). As '.' stands for 'any kind of character' it could be useful to limitate to a set of character such as [0-9] (i.e. \d) \w
Ofcourse the more info provided will yield a more pimped and polished regex. However, with the info provided I was just throwing a bone since it hadn't been mentioned. 👌
 
Top