How do I use RegexReplace to remove last character?

1 favourites
  • 13 posts
From the Asset Store
Customize the animation of character when item changed
  • Hello guys! I use this:

    RegexReplace(Text1.Text, "@", "m", "")

    [/code:109z5p8u]

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

    Example: I have a text with result:

    Test1@Test2@Test3@Test4@Test5@

    So, if i use the code above i get this result each time:

    Try1:

    Test1Test2@Test3@Test4@Test5@

    Try2:

    Test1Test2Test3@Test4@Test5@

    Try3:

    Test1Test2Test3Test4@Test5@

    etc etc

    So i need to use a code to remove from last "@" and all previous characters until the next "@".

    Any solution appreciated, Thanks!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • you could use "left(word, len(word-1))

    it will only keep all of the letters except the last one.

  • 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?

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

    replace will find all the in Text1.text and will replace them with empty strings ( "" ).

  • 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.

  • makotto

    Thanks a lot!!! It's a bit complicate but it's ok, don't worry!

  • makotto

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

    To understand more, we have for example this:

    Test1@Test2@Test3@Test4@Test5@

    When i remove the first "@" to remove all characters until the next one, etc etc.. So to have this:

    Result 1: Test1@Test2@Test3@Test4@

    Result 2: Test1@Test2@Test3@

    Result 3: Test1@Test2@

    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: MyName@Hello@12345@!4_ee$@

    Result 2: MyName@Hello@12345@

    Result 3: MyName@Hello@

    etc etc

    Thanks again!!!

  • 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.

  • 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!

  • If you want to remove "Test5@" 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.

  • Magistross

    Cooooollllllllllllll!!! ; -))) Thanks!

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

    it will only keep all of the letters except the last one.

    There was a slight mistake in Justifun's answer, but the following works perfectly:

    left(word, len(word)-1)[/code:nkzd3l93]
    
    I needed something similar and this helped me out, thanks!
  • Great! ; -)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)