Help with Extracting Numbers from Item Name in Skript (Regex Issue)

  • Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the community!

    Now, what are you waiting for? Join the community now!

  • LOOKING FOR A VERSION OF SKRIPT?

    You can always check out skUnity Downloads for downloads and any other information about Skript!

kyem2009

Member
May 14, 2024
3
0
1
I’m trying to extract only the numbers from an item’s name in Skript. I tried using the following code, but it’s not working, and I keep getting errors. Here is what I have so far:

Code:
on right click:
    set {_name} to name of player's tool
    set {_number} to "%{_name}%"
    replace all characters except "0123456789" in {_number} with ""
    if {_number} is not "":
        set {_number} to {_number} parsed as number
        send "Extracted number from item name: %{_number}%"
    else:
        send "There is no number in the name."


The goal is to remove all non-numeric characters from the item's name and extract the number. For example, if the item is called Sword123, I want to extract 123. However, this script doesn't seem to be working as expected, and I'm unsure how to fix it.

Can anyone help me identify what I’m doing wrong or suggest a better approach for extracting numbers from a string in Skript?

*It was written by chatGPT
 
I’m trying to extract only the numbers from an item’s name in Skript. I tried using the following code, but it’s not working, and I keep getting errors. Here is what I have so far:

Code:
on right click:
    set {_name} to name of player's tool
    set {_number} to "%{_name}%"
    replace all characters except "0123456789" in {_number} with ""
    if {_number} is not "":
        set {_number} to {_number} parsed as number
        send "Extracted number from item name: %{_number}%"
    else:
        send "There is no number in the name."


The goal is to remove all non-numeric characters from the item's name and extract the number. For example, if the item is called Sword123, I want to extract 123. However, this script doesn't seem to be working as expected, and I'm unsure how to fix it.

Can anyone help me identify what I’m doing wrong or suggest a better approach for extracting numbers from a string in Skript?

*It was written by chatGPT
I would think the issue is due to the line: replace all characters except "0123456789" in {_number} with "". This is because, it does not individually check if each character is or isn't 0-9. It's simply checking if the string has a string within it, in the case: "0123456789", not each individually. Try dividing it up into a list to check if it contains a numerical character then see if that works.