Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exracting Date from string...
#1
str s="$desktop$\CONVERTED\LCL-Short-Show02-2009-October12.WAV"

what would the quickest way of getting "10-12-2009" from s?

Thanks,
Jimmy Vig
#2
findrx
#3
I was afraid that it would be something with findrx.

Really I'm not very good with writing the patterns for that kind of stuff. It all looks like chinese to me.

Could you give an example here and maybe some other useful tips on working with the patterns?

Thanks,
jimmy Vig
#4
If you work with strings much, you should learn regular expressions, it is not so difficult. Then you can write code much faster and easier than using find etc. Many regular expression tutorials are on the internet.
#5
I've been google-ing around:

http://regexlib.com/CheatSheet.aspx
http://krijnhoetmer.nl/stuff/regex/cheat-sheet/
http://www.regular-expressions.info/
http://en.wikipedia.org/wiki/Regular_expression
http://www.amk.ca/python/howto/regex/


Looks like I have a bit of studying to do with all this.

Thanks for the push in the right direction. My grampa used to always say, "Give a man a fish, he eats for a day. Teach a man to fish, he eats for life!" Your a wonderful teacher and I look forward to learning so much more. Thank you very much.
-Jimmy Vig
#6
Macro findrx tutorial
Code:
Copy      Help
;Shows how to use function findrx() to find and extract text.

;findrx() is is similar to find().
int i
i=find("Sunday Monday Tuesday" "Monday") ;;find Monday
out i
i=findrx("Sunday Monday Tuesday" "Monday") ;;find Monday
out i

;But findrx() can find not only exact text.
;The second argument (regular expression) can contain special characters (metacharacters) that match certain characters or conditions.
i=findrx("file578.txt" "\d+") ;;find a number. Here \d matches a digit, and + means "one or more". So, \d+ matches one or more digits.
out i

;How to extract the found text?
str s
i=findrx("file578.txt" "\d+" 0 0 s) ;;pass a str variable as 5-th argument, and it will be populated with the match
out i
out s

;What if not found?
i=findrx("file.txt" "\d+") ;;if not found, returns -1
if(i<0) out "not found"

;When whole string must match:
i=findrx("578" "^\d+$") ;;here ^ and $ mean "beginning" and "end"
out i
i=findrx("file578.txt" "^\d+$") ;;does not match
out i

;Find a number where it is whole word:
i=findrx("file123 456.txt" "\b\d+\b" 0 0 s) ;;here \b means "word boundary". Word characters are alphanumeric ASCII characters and _.
out i
out s

;Find a date in a filename:
i=findrx("file01-02-2000.txt" "\d{2}-\d{2}-\d{4}" 0 0 s) ;;here \d{2} means "2 digits"
out i
out s

;If need only year:
i=findrx("file01-02-2000.txt" "\d{2}-\d{2}-(\d{4})" 0 0 s 1) ;;6-th argument tells which submatch (part enclosed in ()) to get into s
out i
out s

;If need month, day and year:
ARRAY(str) a
i=findrx("file01-02-2000.txt" "(\d{2})-(\d{2})-(\d{4})" 0 0 a)
out a[0] ;;whole match
out a[1] ;;submatch 1 (month)
out a[2] ;;submatch 2 (day)
out a[3] ;;submatch 3 (year)

;Find substring consisting of specified characters:
i=findrx("xxx 0x3ea5 yyy" "0x[\dabcdef]+" 0 0 s) ;;here [] is used to specify characters, \d is digit, + means "1 or more". So it finds 0x followed by 1 or more digits and characters abcdef.
out i
out s

;How to use \^$()[].+? and other metacharacters as ordinary characters? (all metacharacters are listed in "Regular expression syntax" topic in QM Help)
i=findrx("file.txt" ".+\.txt" 0 0 s) ;;insert \ before each such character. Here the first . means "any character", however the second . is just . because preceded by \.
out s
i=findrx("xxx file.txt yyy" "\Qfile.txt\E" 0 0 s) ;;or enclose part of regular expression in \Q and \E
out s

Macro replacerx tutorial
Code:
Copy      Help
;Shows how to use function str.replacerx() to find-replace text.

;str.replacerx() is similar to str.findreplace().
str x="Sunday Monday Tuesday"
x.findreplace("Monday" "(here was Monday)")
out x
str y="Sunday Monday Tuesday"
y.replacerx("Monday" "(here was Monday)")
out y

;But it can find not only exact text. It can find text like findrx().
y="file123 456.txt"
y.replacerx("\d+" "(here was a number)")
out y

;What if not found?
y="file.txt"
int n=y.replacerx("\d+" "(here was a number)") ;;replacerx returns number of found matches
if(n=0) out "not found"

;How to use submatches in the replacement string?
y="file01-02-2000.txt"
y.replacerx("(\d{2})-(\d{2})-(\d{4})" "$3-$1-$2") ;;in the replacement string, $number can be used for a submatch
out y ;;converted date from mm-dd-yyyy to yyyy-mm-dd format
#7
Thanks bunches. I still had a lot of troubles clunking around with this so far...but I got it!

Code:
Copy      Help
out
str sFile="$desktop$\CONVERTED\LCL-Short-Show02-2009-October12.WAV"

ARRAY(str) a
int i=findrx(sFile "\d{2}-(\d{4})-([\w)]+)(\d{2})" 0 0 a)
str sDate.from(a[2] "-" a[3] "-" a[1])
DATE d=sDate
out d

Your examples didn't mention anything about using "\w" for words or how the "+" sign works, or that things in the parenthetical statements make up the indexes in the array. I found some good information here: http://www.regular-expressions.info/charclass.html that helped put it all together.

Thanks bunches. You were a big help and I finally think I can start using this stuff. It is pretty powerful!!
Jimmy Vig
#8
i am a little bit rusty, when it comes to quickly writing code with regex and findrx.
please guide me into the right direction. Confusedhock:

i have links with different hosts and need to wrap bbcode around found links from defined hosts.

like:
Code:
Copy      Help
http://aaaaaaaa.com/files/389656480/BTh1.rar
http://aaaaaaaa.com/files/389656484/BTh2.rar
http://www.bbbbbbbbbbbbbb.com/1910306847/BTh1.rar
http://www.bbbbbbbbbbbbbb.com/1910306858/BTh2.rar
http://ccccccccccccc.com/files/jv9pb90bh
http://ccccccccccccc.com/files/jv934rsxb


result should be:
Code:
Copy      Help
[box title=aaaaaaaa class=tborder]
http://aaaaaaaa.com/files/389656480/BTh1.rar
http://aaaaaaaa.com/files/389656484/BTh2.rar
[/box]
[box title=bbbbbbbbbbbbbb class=tborder]
http://www.bbbbbbbbbbbbbb.com/1910306847/BTh1.rar
http://www.bbbbbbbbbbbbbb.com/1910306858/BTh2.rar
[/box]
[box title=ccccccccccccc class=tborder]
http://ccccccccccccc.com/files/jv9pb90bh
http://ccccccccccccc.com/files/jv934rsxb
[/box]

thanks.
pi
#9
Macro Macro1386
Code:
Copy      Help
out
str s=
;http://ccccccccccccc.com/files/jv934rsxb
;http://aaaaaaaa.com/files/389656480/BTh1.rar
;http://aaaaaaaa.com/files/389656484/BTh2.rar
;http://www.bbbbbbbbbbbbbb.com/1910306847/BTh1.rar
;http://ccccccccccccc.com/files/jv9pb90bh
;http://aaaaaaaa.com/files/389656484/BTh3.rar

;sort
ARRAY(str) a=s; a.sort(2); s=a

;if(!s.end("[]")) s+"[]" ;;don't need this if sort

out s.replacerx("^(http://(?:[\w\-]+\.)?(.+?)\.\w+)/.+[](\1/.+[])*" "[box title=$2 class=tborder][]$0[/box][]" 8)
out s

\1 matches text of subpattern 1.
#10
sorting is a good idea.
and since you are so quick Big Grin
the hosts will come from an array.

ARRAY(str) hosts="aaaaaaaa[]bbbbbbbbbbbbbb[]ccccccccccccc"
ARRAY(str) hostsTitles="Aaaaaaaa home of Somehing[]Bbbbbbbbbbbbbb - Index[]ccccccccccccc - keeping letters short"
pi
#11
Macro Macro1386
Code:
Copy      Help
out
str s=
;http://ccccccccccccc.com/files/jv934rsxb
;http://aaaaaaaa.com/files/389656480/BTh1.rar
;http://aaaaaaaa.com/files/389656484/BTh2.rar
;http://www.bbbbbbbbbbbbbb.com/1910306847/BTh1.rar
;http://ccccccccccccc.com/files/jv9pb90bh
;http://aaaaaaaa.com/files/389656484/BTh3.rar

;sort
ARRAY(str) a=s; a.sort(2); s=a

;if(!s.end("[]")) s+"[]" ;;don't need this if sort

out s.replacerx("^(http://(?:[\w\-]+\.)?(.+?)\.\w+)/.+[](\1/.+[])*" "[box title=$2 class=tborder][]$0[/box][]" 8)
out s

out "---------"
ARRAY(str) hosts="aaaaaaaa[]bbbbbbbbbbbbbb[]ccccccccccccc"
ARRAY(str) hostsTitles="Aaaaaaaa home of Somehing[]Bbbbbbbbbbbbbb - Index[]ccccccccccccc - keeping letters short"
int i
for i 0 hosts.len
,s.findreplace(F" title={hosts[i]} " F" title={hostsTitles[i]} ")

out s
#12
Function n__autoformat
Code:
Copy      Help
int i
for i 0 hosts.len
,s.findreplace(F" title={hosts[i]} " F" title={hostsTitles[i]} ")
,out hostsTitles[i]
out s

what is F ?
the renaming does not work for me.

other than that its quite nice!!!
pi
#13
it is easier str.format in QM 2.3.2.
#14
Gintaras Wrote:it is easier str.format in QM 2.3.2.

i made one mistake, that was adding quotes for correct bbcode formating and forgot to add the quotes in renaming.
so now i am really happy. thanks for helping me.

i still don't know what the F is for. Confusedhock:
pi
#15
_pi Wrote:i still don't know what the F is for. Confusedhock:


http://www.quickmacros.com/help/Languag ... TRING.html
#16
Quote:Use operator F to insert variables and other expressions in strings.

cool! thanks again.
pi
#17
i need to tweak a.sort

the links indeed come unsorted. how to sort by string content after last slash?

/Fiscd1.part2.rar
/Fiscd2.part2.rar
/Fiscd2.part1.rar
/Fiscd1.part1.rar

it should try to sort first by Fiscd and then part

http://www.eeeeeeeeeeee.com/1910314228/Fiscd1.part4.rar
http://www.eeeeeeeeeeee.com/1910314232/Fiscd2.part6.rar
http://www.eeeeeeeeeeee.com/1910314236/Fiscd1.part5.rar
http://www.eeeeeeeeeeee.com/1910314237/Fiscd1.part2.rar
http://www.eeeeeeeeeeee.com/1910314238/Fiscd1.part6.rar
http://www.eeeeeeeeeeee.com/1910314239/Fiscd1.part3.rar
http://www.eeeeeeeeeeee.com/1910314240/Fiscd2.part4.rar
http://www.eeeeeeeeeeee.com/1910314243/Fiscd2.part5.rar
http://www.eeeeeeeeeeee.com/1910314244/Fiscd1.part1.rar
http://www.eeeeeeeeeeee.com/1910314246/Fiscd1.part8.rar
http://www.eeeeeeeeeeee.com/1910314247/Fiscd1.part7.rar
http://www.eeeeeeeeeeee.com/1910314248/Fiscd2.part2.rar
http://www.eeeeeeeeeeee.com/1910314250/Fiscd2.part3.rar
http://www.eeeeeeeeeeee.com/1910314252/Fiscd2.part1.rar

thanks!
pi
#18
Use your sort callback function.

Macro Macro1386
Code:
Copy      Help
out

str s=
;http://www.eeeeeeeeeeee.com/1910314228/Fiscd1.part4.rar
;http://www.eeeeeeeeeeee.com/1910314232/Fiscd2.part6.rar
;http://www.eeeeeeeeeeee.com/1910314236/Fiscd1.part5.rar
;http://www.eeeeeeeeeeee.com/1910314237/Fiscd1.part2.rar
;http://www.eeeeeeeeeeee.com/1910314238/Fiscd1.part6.rar
;http://www.eeeeeeeeeeee.com/1910314239/Fiscd1.part3.rar
;http://www.eeeeeeeeeeee.com/1910314240/Fiscd2.part4.rar
;http://www.eeeeeeeeeeee.com/1910314243/Fiscd2.part5.rar
;http://www.eeeeeeeeeeee.com/1910314244/Fiscd1.part1.rar
;http://www.eeeeeeeeeeee.com/1910314246/Fiscd1.part8.rar
;http://www.eeeeeeeeeeee.com/1910314247/Fiscd1.part7.rar
;http://www.eeeeeeeeeeee.com/1910314248/Fiscd2.part2.rar
;http://www.eeeeeeeeeeee.com/1910314250/Fiscd2.part3.rar
;http://www.eeeeeeeeeeee.com/1910314252/Fiscd2.part1.rar

ARRAY(str) a=s
a.sort(0 Function177)

s=a
out s

Function Function177
Code:
Copy      Help
function# param str&a str&b

ret StrCompare(a+findcr(a '/')+1 b+findcr(b '/')+1 1)
#19
i hope i bother the last time.
how to combine that with the first sorting, because now the hosts are not sorted.
pi
#20
In callback function, split the url, and compare at first paths, then filenames (if paths equal).
#21
i am sorry, but i don't understand (rusty) to make that.

the array hosts has the default ordering. so i need first to sort the links by hosts and then by filename.
and that result should be enclosed in bbcode.

my problem is that i currently don't understand the callback function.
pi
#22
From help:

The callback function must compare a and b, and return:

-1 if a must be placed before b;

1 if a must be placed after b;

0 if there is no difference.
#23
one thing i understand is that once you have assigned your multi-line text to an array, that callback function is running line-number times.
right?

does a callback function have an internal counter?
pi
#24
No. If need counter, use param.

macro:
int i
a.sort(0 func &i)

callback function:
function# int&i str&a str&b
i+1 ;;now i is counter


Forum Jump:


Users browsing this thread: 1 Guest(s)