Have regex only return one item?

Discuss RoboTask here

Have regex only return one item?

Postby kunkel321 » Sat Mar 21, 2020 4:47 pm

Hi there,
The Task at the bottom is meant to locate the time in a string of text, then save it to a variable for re-use. I don't want it to get all of the subexpressions though... I only want it to get "2:45pm." Ideas how to do this?

EDIT: Actually, it looks like checking the "No AutoCapture" box is the answer...
Second Edit: Or maybe not. (See Oleg's reply)

---------------------------
RoboTask: TimeGetter
---------------------------
Time = 2:45pm,2:45pm,2:4,pm,pm
---------------------------
OK
---------------------------

fyi This was made from copying Oleg's IP Address task :- }

Code: Select all
;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
 
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|-1
CatID=INTEGER|327331060
Comment=STRINGLIST
ContinueOnError=INTEGER|0
DoNotStopWhenShutdown=INTEGER|0
Events=FOLDER
ExternalName=STRING|"Task3"
Hide=INTEGER|0
ID=INTEGER|1840723945
LogOnAsUser=INTEGER|1
Name=STRING|"TimeGetter"
OnErrorTaskID=INTEGER|-1
Priority=INTEGER|3
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
ToLog=INTEGER|3
UnicodeFormat=INTEGER|1
WriteGeneralLog=INTEGER|0

[Actions]
Action1=FOLDER
Action2=FOLDER
Action3=FOLDER
Action4=FOLDER
Action5=FOLDER
Action6=FOLDER

[Actions\Action1]
ActionID=STRING|"A_REGEXP_MATCH"
Enabled=INTEGER|-1
Name=STRING|"RegExp reTime Match"
Params=FOLDER

[Actions\Action1\Params]
anchored=STRING|"0"
case=STRING|"1"
extended=STRING|"0"
line00000000=STRING|"The time will be 2:45pm next Wednesday."
linecount=STRING|"1"
multiline=STRING|"0"
noautocapture=STRING|"0"
pattern=STRING|"\b(([0-2]?[0-9]:[0-5])[0-9](\s?(pm|am|p|a)?)|[12]?[0-9](pm|am|p|a))\b"
savesubexpression=STRING|"1"
singleline=STRING|"0"
subexpressionpos=STRING|"0"
subexpressionvar=STRING|"reTime"
ungreedy=STRING|"0"
useexternal=STRING|"0"
variable=STRING|"found"

[Actions\Action2]
ActionID=STRING|"A_FLOW_IF"
Enabled=INTEGER|-1
Name=STRING|"If Then"
Params=FOLDER

[Actions\Action2\Params]
case=STRING|"0"
connection=STRING|"0"
count=STRING|"1"
operator=STRING|"0"
type=STRING|"3"
value1=STRING|"{found}"

[Actions\Action3]
ActionID=STRING|"A_DIALOG_MESSAGE"
Enabled=INTEGER|-1
Name=STRING|"Show ""Time = {reTime}"""
Params=FOLDER

[Actions\Action3\Params]
icon=STRING|"1"
msg0=STRING|"Time = {reTime}"
msgcount=STRING|"1"
playsound=STRING|"0"
showmessage=STRING|"1"

[Actions\Action4]
ActionID=STRING|"A_FLOW_ELSE"
Enabled=INTEGER|-1
Name=STRING|"Else"

[Actions\Action5]
ActionID=STRING|"A_DIALOG_MESSAGE"
Enabled=INTEGER|-1
Name=STRING|"Show ""Not found."""
Params=FOLDER

[Actions\Action5\Params]
icon=STRING|"0"
msg0=STRING|"Not found."
msgcount=STRING|"1"
playsound=STRING|"0"
showmessage=STRING|"1"

[Actions\Action6]
ActionID=STRING|"A_FLOW_ENDIF"
Enabled=INTEGER|-1
Name=STRING|"End If"

[Events]
Event1=FOLDER

[Events\Event1]
Enabled=INTEGER|-1
EventID=STRING|"E_GENERAL_HOTKEY"
Name=STRING|"Shift+Ctrl+Alt+C"
Params=FOLDER
UniqueID=INTEGER|1635334739

[Events\Event1\Params]
hotkey=STRING|"57411"

Last edited by kunkel321 on Sun Mar 22, 2020 6:55 pm, edited 1 time in total.
kunkel321
 
Posts: 10
Joined: Sun Mar 15, 2020 5:23 pm

Re: Have regex only return one item?

Postby Oleg » Sun Mar 22, 2020 10:19 am

I made some corrections in your task. Test it please.
When you save found expression into variable the RegExp Match action saves expression as comma-separated string: a zero element is the whole expression. The next elements are subexpressions in order.
Each expression in round brackets "(...)" is a sub-expression.

Test the task. Save the text of the task to a file and use menu Task->Import to import the task into RoboTask.
Do not forget to enable the task after importing. I changed the step#3 only ( {reTime} --> {reTime(0)} )
Code: Select all
;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
 
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|0
CatID=INTEGER|1075658632
Comment=STRINGLIST
ContinueOnError=INTEGER|0
DoNotStopWhenShutdown=INTEGER|0
Events=FOLDER
ExternalName=STRING|"Task1137"
Hide=INTEGER|0
ID=INTEGER|1840723945
LogOnAsUser=INTEGER|1
Name=STRING|"TimeGetter (corrected)"
OnErrorTaskID=INTEGER|-1
Priority=INTEGER|3
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
ToLog=INTEGER|3
UnicodeFormat=INTEGER|1
WriteGeneralLog=INTEGER|0

[Actions]
Action1=FOLDER
Action2=FOLDER
Action3=FOLDER
Action4=FOLDER
Action5=FOLDER
Action6=FOLDER

[Actions\Action1]
ActionID=STRING|"A_REGEXP_MATCH"
Enabled=INTEGER|-1
Name=STRING|"RegExp reTime Match"
Params=FOLDER

[Actions\Action1\Params]
anchored=STRING|"0"
case=STRING|"1"
extended=STRING|"0"
line00000000=STRING|"The time will be 2:45pm next Wednesday."
linecount=STRING|"1"
multiline=STRING|"0"
noautocapture=STRING|"0"
pattern=STRING|"\b(([0-2]?[0-9]:[0-5])[0-9](\s?(pm|am|p|a)?)|[12]?[0-9](pm|am|p|a))\b"
savesubexpression=STRING|"1"
singleline=STRING|"0"
subexpressionpos=STRING|"0"
subexpressionvar=STRING|"reTime"
ungreedy=STRING|"0"
useexternal=STRING|"0"
variable=STRING|"found"

[Actions\Action2]
ActionID=STRING|"A_FLOW_IF"
Enabled=INTEGER|-1
Name=STRING|"If Then"
Params=FOLDER

[Actions\Action2\Params]
case=STRING|"0"
connection=STRING|"0"
count=STRING|"1"
operator=STRING|"0"
type=STRING|"3"
value1=STRING|"{found}"

[Actions\Action3]
ActionID=STRING|"A_DIALOG_MESSAGE"
Enabled=INTEGER|-1
Name=STRING|"Show ""Time = {reTime(0)}"""
Params=FOLDER

[Actions\Action3\Params]
icon=STRING|"1"
msg0=STRING|"Time = {reTime(0)}"
msgcount=STRING|"1"
playsound=STRING|"0"
showmessage=STRING|"1"

[Actions\Action4]
ActionID=STRING|"A_FLOW_ELSE"
Enabled=INTEGER|-1
Name=STRING|"Else"

[Actions\Action5]
ActionID=STRING|"A_DIALOG_MESSAGE"
Enabled=INTEGER|-1
Name=STRING|"Show ""Not found."""
Params=FOLDER

[Actions\Action5\Params]
icon=STRING|"0"
msg0=STRING|"Not found."
msgcount=STRING|"1"
playsound=STRING|"0"
showmessage=STRING|"1"

[Actions\Action6]
ActionID=STRING|"A_FLOW_ENDIF"
Enabled=INTEGER|-1
Name=STRING|"End If"

[Events]
Event1=FOLDER

[Events\Event1]
Enabled=INTEGER|-1
EventID=STRING|"E_GENERAL_HOTKEY"
Name=STRING|"Shift+Ctrl+Alt+C"
Params=FOLDER
UniqueID=INTEGER|-1779785127

[Events\Event1\Params]
hotkey=STRING|"57411"

Oleg Yershov
Oleg
Site Admin
 
Posts: 2511
Joined: Thu Jan 01, 1970 1:00 am

Re: Have regex only return one item?

Postby kunkel321 » Sun Mar 22, 2020 6:59 pm

Thanks Oleg! I actually did read that part of the manual... I guess I just needed an example before I could understand it. :mrgreen:
kunkel321
 
Posts: 10
Joined: Sun Mar 15, 2020 5:23 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 18 guests

cron