Help with Regexp

● ARCHIVED · READ-ONLY
Started by Gleen 7 posts View original ↗
  1. I have a string like this:



    Code:
    <require: WEAPON id: X lvl: Y>
    I want to return WEAPON, X and Y so i'm scanning this string using the following Regexp:



    Code:
    /<require: (\w) id: ([+-]\d+) lvl: ([+-]\d+)%?>/i
    When i run my code, it returns an empty array, Im assuming it's caused by the multiple grouping. How can i do that regexp?
  2. you're only searching for one letter when you want to get Weapon ;)

    this one works just fine for me:



    Code:
    /<require: ([\w]+) id: ([\+|-][\d]+) lvl: ([\+|-][\d]+)%?>/i
    I was searching in this string:



    Code:
    <require: WEAPON id: +10 lvl: +2>
    Don't forget to put a backslash before the plus, since it usually has another meaning ;)

    (I don't know why you'd need the plus/minus there anyway, since it's item Ids and Levels, and those can't be less than 1, can they?)
  3. Thanks mobychan, your solution worked perfectly. I'm still learning regular expressions and man, what a pain... Yeah, you're right, no need for plus/minus :)
  4. No problem ^^

    Most of the time I just take the string I want it to find and edit it at the points I need ^^

    Took me a while too to get how regex work and I'm still not that good at it, but it's enough ^^
  5. http://rubular.com/

    Use rubular when your regexp don't work as expected, you can test them.



    Code:
    /<require: ([\w]+) id: ([\+|-][\d]+) lvl: ([\+|-][\d]+)%?>/i
    you dont need to use lists on this code



    Code:
    /<require: (\w+) id: ([+-]?\d+) lvl: ([+-]?\d+)%?>/i
    Will work exactly the same way

    he just forget the + on (\w+), since \w will match only one character, so to match a whole word he needs \w+

    also i used [+-]? to make the +/- sign optional

    In fact i would use:



    Code:
    /<require: (\w+) id: (\d+) lvl: (\d+)>/i
    since it's looks like that you will use only the weapon/armor ID and level, and there's no need to make these value negative or % (unless you plan something different)
  6. Thanks, my question has been solved so any mod can close this.
  7. This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.