Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex madness...

after 2 hours of pulling my hairs off, i decide to ask the master Gintaras, though i'd like to understand why my methods fail.

Easy case : I have files that look like

I spend 2 hours trying to remove the LAST digit(s) of the files names.
findrx(_s "^(.*)(\d+)$) 0 0 match 1)

but anyway i try (this is only one example i tried, i tried all methods i could), match variable always only contains 1 digit at the end, where it should be equal to

Seems the \d+ which should catch one or more digit (+ sign) does only take the first one.

What did I not understand?
Probably because .* matches as much as possible, including digits. Try .*? or [^\d]*

Or _s.rtrim("0123456789-")
rtrim did it, but frustrating in losing so much time when syntax is correct and
not behaving as supposed.

But i've got other questions so I carry on (not all regex related).

I must test a word to a list.

1. I used the find method, array method (foreach...), the IStringMap method; what is the most efficient (quickest and lightest)???

2. I want make "batch" replace in a string, looking sometimes for several replacements in the same string.

ex :
str s="v xv XVI"
ss.replacerx("xix" "XIX" 2|8|32)
ss.replacerx("xx" "XX" 2|8|32)

the replacerx line are repeated for each and the file is now pretty long and it becomes inconvenient.

I'm sure there is a better way...
Macro Macro
Copy      Help
str Coll=
str match
foreach _s Coll
,findrx(_s ".*?(?=\d*$)" 0 0 match);;0 or more any character non-greedy (so doesn't capture the digits at the end), followed by 0 or more digits which are followed by end of the line
,out match
IStringMap would be fastest to find in a big list. But if need to replacerx, I probably would use array.

str s.getfile(...)
ARRAY(str) a=s
int i
for i 0 a.len

If it is too slow, can be optimized to make maybe 2 times faster.
Thanks stupomer, i was close to that but for some reason, missed the ? in.*. Big Grin

Gintaras, my list of replacement is containing about 500 pairs, so I chose a IStringMap, which seems pretty fast.
Ty both of you

Forum Jump:

Users browsing this thread: 1 Guest(s)