Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Determine string variable type
#1
Hello everyone,

I need to make a judgment, but can't get the right result, I hope someone can point out the mistake. Thanks in advance

If s is a number or variable name, out s, else out "s"

 Expected output:
 Case 1    a2
 Case 2   _b
 Case 3    3
 
 Case 4   "2a"
 Case 5   "C:\Users\Administrator\Desktop\test.txt"
 Case 6   "C:\Users\Administrator\Desktop"
 Case 7   "世界"


Macro q1
Code:
Copy      Help
;str s="a2" ;;Case 1
;str s="_b" ;;Case 2
;str s="3" ;;Case 3

str s="2a" ;;Case 4
;str s="C:\Users\Administrator\Desktop\test.txt" ;;Case 5
;str s="C:\Users\Administrator\Desktop" ;;Case 6
;str s="世界" ;;Case 7

if(findrx(s "^[a-zA-Z_][a-zA-Z0-9_]*") or findrx(s "\d+"))
,out s
else
,out F"''{s}''"    

;Expected output:
;Case 1    a2
;Case 2   _b
;Case 3    3
;
;Case 4   "2a"
;Case 5   "C:\Users\Administrator\Desktop\test.txt"
;Case 6   "C:\Users\Administrator\Desktop"
;Case 7   "世界"
#2
this should work for you
try this rx
Code:
Copy      Help
if(findrx(s[i] "\D+(?=\d)|\d+(?!.+)|\_(?=\D+)")>=0)
 full code example
Code:
Copy      Help
ARRAY(str) s
s[]="a2" ;;Case 1
s[]="_b" ;;Case 2
s[]="3" ;;Case 3

s[]="2a" ;;Case 4
s[]="C:\Users\Administrator\Desktop\test.txt" ;;Case 5
s[]="C:\Users\Administrator\Desktop" ;;Case 6
s[]="世界" ;;Case 7

for int'i 0 s.len
,if(findrx(s[i] "\D+(?=\d)|\d+(?!.+)|\_(?=\D+)")>=0)
,,out s[i]
,else
,,out F"''{s[i]}''"
#3
This case Unable to test normally

str s="C:\APK\Website 2 APK Builder Pro.exe"

str s="C:\Users\Administrator\Desktop\libxl_ke.exe"

str s="C:\Users\Administrator\Desktop\工作簿1.xlsx"

str s="C:\Users\Administrator\Desktop\LibXL.3.9.3"
#4
try this one
Code:
Copy      Help
ARRAY(str) s
s[]="a2" ;;Case 1
s[]="_b" ;;Case 2
s[]="3" ;;Case 3
s[]= "a"
s[]="2a" ;;Case 4
s[]="C:\Users\Administrator\Desktop\test.txt" ;;Case 5
s[]="C:\APK\Website 2 APK Builder Pro.exe" ;;Case 6
s[]="C:\Users\Administrator\Desktop\libxl_ke.exe"
s[]="C:\Users\Administrator\Desktop\工作簿1.xlsx"
s[]="世界" ;;Case 7

str pattern="^(?!.*\:\\|\d\D|[\x80-\xff]).*$"
for int'i 0 s.len
,if(findrx(s[i] pattern)>=0)
,,out s[i]
,else
,,out F"''{s[i]}''"

outputs
a2
_b
3
a
"2a"
"C:\Users\Administrator\Desktop\test.txt"
"C:\APK\Website 2 APK Builder Pro.exe"
"C:\Users\Administrator\Desktop\libxl_ke.exe"
"C:\Users\Administrator\Desktop\工作簿1.xlsx"
"世界"
#5
@kevin
very Great. Thanks a lot


Forum Jump:


Users browsing this thread: 2 Guest(s)