I have a question about RegEx if anyone can help me out.
This is the current tag I am trying to make.
<Example\S\s(d*)\S>
How do I make this part read any amount numbers?
\s(d*)\S
For Example
<Example\S\s(d*)\S\s(d*)\S\s(d*)\S\s(d*)\S\s(d*)\S\s(d*)\S\s(d*)\S\s(d*)\S\s(d*)\S\s(d*)\S\s(d*)\S\s(d*)\S\s(d*)\S\s(d*)\S\s(d*)\S>
But hopefully a lot shorter.
Doesn't, the * means ANY AMOUNT???
Well, it seems like a strange code for which you're asking, but something like this should work:
/<Example\S((\s\d+\S)*)>/im
It should match things like this:
<ExampleA 15B 7k 9D>
$1 should match " 15B 7k 9D", and $2 would probably match " 9D"
In order to get every unit, then you could scan $1.
@M.A.~ B.E.A.Utiful Thank you so much. Works perfectly.
One more question though about that, does $2 just print the last expression then? That's what it looks like it's doing but I just want to make sure.
@Galenmereth~ Yep that's where I do all of my testing. Thank you though.
Whenever you match a regex, $1 to $9 will contain successive groups that matched. So the first group matched goes in $1, the second group matched goes in $2, and so on.
Yes normally you would be correct but that is not what I was asking. With the set-up that M.A. gave that is not the case. $2 is returning what seems to be the last number in that set-up.
For example <Example: 2, 4, 56, 77, 88, 99, 101>
With his formula $2 would return 101, not 4.
I'd rather write a clean and simple regex and let ruby handle the input parsing, rather than try to figure out how to write a complicated regex.
I do agree normally but for what I need this is perfect.
I am writing a prefix and suffix script and I want to allow the game designer to control what prefixes and suffixes are allowed via note tags.
Thanks again to everyone.
Can someone tell me how to extract the numbers from the string though.
For this example
<Example: 2, 3, 4, 5, 6, 7>
the current method would create a string with $1 that looks like so.
" 2, 3, 4, 5, 6, 7"
I need to return all of the digits to an array and I have never worked with String Methods before.
def random_suffix if @random_suffix.nil? @random_suffix = [] if @note =~ /<rand[-_ ]?suffix:\s*(.*)\s*>/i $1.split(',').each {|i|@random_suffix << i.to_i} else @random_suffix << 0 end end return @random_suffix.sampleendGrabbed the above from my random affix script for Tsukihime's Instance Items. Since you're going through a string to fill an array for numbers, this should show enough to allow you to do it on your own, but step by step it would be.1: Create your array.
2: Do your note checking thing
3: Use the .split function on the resulting string(this creates an array full of strings)
4: Loop through it turning each string into a number and depositing it into the array you created.
@ Selchar~ I just figured it out, thank you very much though. (although I got there a different way)
Here is the method I am using.
#----------------------------------------------------------------------------- # * Create Prefix #----------------------------------------------------------------------------- def create_prefix(id) pre = [] note = /\s*(\d*)\S*/im p_note =/<Prefix\S((\s\d*\S)*)>/im base = $data_weapons[id] base.note.scan(p_note) prefixes = $1 p_array = prefixes.scan(note) p_array.size.times { |i| pre.push p_array[0] } prefix = p_array[rand(p_array.size - 2)] return prefix[0].to_i endI used .scan
Merged those posts for you.
You could also scan, as modern_algebra suggested. The following will return an array of integers based on the values matched.
rx = /<Example\S\s*(\d+(?:\s*\d+\S)*\s*)+>/isx = "<Example: 22, 44, 66, 88>"sx.match(rx) { $1.scan(/\d+/).collect { |v| v.to_i } }If you don't care about what that \S matches, though, why is it even in the regexp? You can match commas and colons directly (unless paired with another special character) without even escaping them. The above regexp would break if you ended with a single digit number that wasn't followed by a non whitespace character to complete the match.I've used this type of regexp for infinite digit match with comma separators.
/<example:\s*(\d+(?:\s*,\s*\d+)*)>/iThen scan it to retrieve the data, as above. Note that scan also generates matchdata, so make sure you are done with the previous matchdata when you run it (or store and clone the old match).EDIT: looks like you got it figured out
@Mithran~ Yeah but you helped me shorten the code a lot. I will post the new method in just a bit. Thanks again everyone.
~edit
Here is the method
#----------------------------------------------------------------------------- # * Create Prefix #----------------------------------------------------------------------------- def create_prefix(id) base = $data_weapons[id] p_note = /<Prefix\S*\s*(\d+(?:\S*\s*\d+)*)+>/im pre = base.note.match(p_note) { $1.scan(/\d+/).collect { |i| i.to_i } } prefix = pre[rand(pre.size)] return prefix endI just noticed something... Is base a keyword? The reason I ask is, it blue in the code above. I'm not getting any problems now but that doesn't mean i won't later.
Thanks for that save Tsuki, but I don't know why anyone would use two different prefix scripts.(it really wouldn't surprise me tough)
I'm just saying if you start getting into the habit of just accepting any strings as your note-tag ID, you run the risk of writing things like this in the future without thinking about it
/<place\S*\s*/ ... >/im/<rad\S*\s*/ ... >/im/<mat\S*\s*/ ... >/imAnd then suddenly EVERY word that begins with "place", "rad", or "mat" will be captured by your tag. It is better to provide stricter matching rules and force users to follow the rules you impose, rather than allow them to write whatever they want."Prefix" is a fairly long string, but there's no reason why I wouldn't want to use the word "prefix_random" or something in the future to denote a random prefix.
yeah, at least place a special character like a : I think... so like mat:
because if it's just mat, then if a tag says something like, materials, it will be captured too...
Yeah those are both great point that I didn't think about. I usually use : after the tag like your example Shana. But like Tsuki said and I am guilty of it, but I always try to make it easy on the designer but things like that should be sacrificed to make sure there are no issues. That is much smarter way of looking at it. Thank you both.
I don't think 'hey you can type in "Prefix" or "PREFIXATION" or "PREFIXHDIAUDSIADHA"' would make any difference for a game developer using your script.
Unless you mean something else.
When it comes to a tag name, shorter names would be easier to type and thus somewhat easier to use, but realistically anything you use will probably be less than 100 characters long anyways.
LOL. I would never do a 100 character tag, but I get your point. That would be funny though. No I meant adding the : back into the tags to help make sure I am grabbing the right tags.