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

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
_s=Name.OfTheFile001
_s=NameOf.The_9File-02
_s=Name.OfThe.File.3

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
Name.OfTheFile
NameOf.The_9File-
Name.OfThe.File.

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

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

Or _s.rtrim("0123456789-")
#3
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...
#4
Macro Macro
Code:
Copy      Help
str Coll=
;Name.OfTheFile001
;NameOf.The_9File-02
;Name.OfThe.File.3
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
#5
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
,a[i].replacerx(...)
s=a
s.setfile(...)

If it is too slow, can be optimized to make maybe 2 times faster.
#6
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)