How do I use RegexReplace to remove last character?

Get help using Construct 2

Post » Wed Sep 28, 2016 7:41 pm

Hello guys! I use this:
Code: Select all
RegexReplace(Text1.Text, "@", "m", "")

but it removes first "@" character. What can i do to remove the last "@" character each time?

Example: I have a text with result:
[email protected]@[email protected]@[email protected]

So, if i use the code above i get this result each time:
Try1: [email protected]@[email protected]@
Try2: [email protected]@[email protected]
Try3: [email protected]@
etc etc

So i need to use a code to remove from last "@" and all previous characters until the next "@". :roll:
Any solution appreciated, Thanks!
Last edited by odiusfly on Thu Sep 29, 2016 11:45 am, edited 4 times in total.
B
52
S
32
G
14
Posts: 242
Reputation: 12,333

Post » Wed Sep 28, 2016 10:53 pm

you could use "left(word, len(word-1))

it will only keep all of the letters except the last one.
B
87
S
44
G
16
Posts: 1,005
Reputation: 17,938

Post » Thu Sep 29, 2016 2:48 am

Are you sure? What is "word" ?
In my sample i have "Result1" As text.. So even i use this not working: "left(Result1.Text, len(Result1.Text-1))"
Am i wrong?
B
52
S
32
G
14
Posts: 242
Reputation: 12,333

Post » Thu Sep 29, 2016 3:32 am

You can use: replace( Text1.text, "@", "")

replace will find all the @ in Text1.text and will replace them with empty strings ( "" ).
B
28
S
10
G
10
Posts: 97
Reputation: 6,584

Post » Thu Sep 29, 2016 4:07 am

Now, if you must have the functionality to remove each "@" one at a time, this gets more complicated.
I couldn't find a good way to do it in the manual (based on the provided tools), so I wrote a little function to do it.
Our ReverseRegexReplace function works this way:
    You pass a text to the function
    It inverts the text (by calling an "InvertText" function)
    It uses RegexReplace to get rid of the first "@" (which was the last on the original string)
    It inverts the string again (because inverting it twice restores the original order)
    Sets the text object to the String value.

Here is the CAPX with the function implementation. It works, but it can be messy due to the nesting:
https://www.dropbox.com/s/riemcuosi1z2i46/invertStringAndRegex.capx?dl=0
If someone finds an easier way to do it, please share. If you have questions, let me know, hope this helps.
B
28
S
10
G
10
Posts: 97
Reputation: 6,584

Post » Thu Sep 29, 2016 10:30 am

@makotto

Thanks a lot!!! It's a bit complicate but it's ok, don't worry! ;)
B
52
S
32
G
14
Posts: 242
Reputation: 12,333

Post » Thu Sep 29, 2016 11:40 am

@makotto

Sorry, i forgot!!! :o How can i remove all characters before "@" until the next "@"?

To understand more, we have for example this:
[email protected]@[email protected]@[email protected]

When i remove the first "@" to remove all characters until the next one, etc etc.. So to have this:
Result 1: [email protected]@[email protected]@
Result 2: [email protected]@[email protected]
Result 3: [email protected]@
etc etc

NOTE: "Test" names are an example.. I talk in case you want to use a code with the name: "Text" and a number, etc etc

Another example can be:
Result 1: [email protected]@[email protected][email protected]
Result 2: [email protected]@[email protected]
Result 3: [email protected]@
etc etc

Thanks again!!! 8-)
B
52
S
32
G
14
Posts: 242
Reputation: 12,333

Post » Fri Sep 30, 2016 2:14 pm

We can do that, we just need to change the function a little bit.

Here is the CAPX with the modified function:
https://www.dropbox.com/s/tu2bhky5f5hjdhp/invertStringAndRegex.capx?dl=0

Let me know if you have other questions.
B
28
S
10
G
10
Posts: 97
Reputation: 6,584

Post » Fri Sep 30, 2016 2:35 pm

@makotto

Awesome!!! Big THANKS my friend.. I test it and i see a tiny issue: The code works until you goes at the last "@".
It remove the last "@" character only without remove "one" word. No need to upload a new capx, you can write here what code line need to change.. Thanks again! ;)
B
52
S
32
G
14
Posts: 242
Reputation: 12,333

Post » Fri Sep 30, 2016 3:37 pm

If you want to remove "[email protected]" from your string, and if you're certain the string will always end with @, this pattern will do it "[^@]*@$".

So RegexReplace(Text1.Text, "[^@]*@$", "m", ""). Note that with the "m" flag, the "$" will match each end of line instead of only the end of the string.
B
71
S
30
G
25
Posts: 984
Reputation: 19,503

Next

Return to How do I....?

Who is online

Users browsing this forum: Armench, Artcadev, farsen, Yahoo [Bot] and 14 guests