- Posts: 5
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Extracting part of string from the answer for validation
- arg0naut91
- Topic Author
- Offline
- New Member
Less
More
4 years 7 months ago - 4 years 7 months ago #187245
by arg0naut91
Extracting part of string from the answer for validation was created by arg0naut91
I'd like to extract part of the string from the answer.
For instance, if "XXX - Y99" is the answer, I'd like to extract the second part ("Y99") and validate if it is equal to TOKEN:ATTRIBUTE_2.
Is something like that feasible?
Regular expression in itself is not a problem (in my case even something like '/Y.*/' would work), but I don't know how to extract it from the answer (e.g. from {q1.shown}).
Any ideas?
P.S. For some reason I cannot use Javascript so this is not an option.
For instance, if "XXX - Y99" is the answer, I'd like to extract the second part ("Y99") and validate if it is equal to TOKEN:ATTRIBUTE_2.
Is something like that feasible?
Regular expression in itself is not a problem (in my case even something like '/Y.*/' would work), but I don't know how to extract it from the answer (e.g. from {q1.shown}).
Any ideas?
P.S. For some reason I cannot use Javascript so this is not an option.
Last edit: 4 years 7 months ago by arg0naut91.
The topic has been locked.
- holch
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 11758
- Thank you received: 2753
4 years 7 months ago #187246
by holch
I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.
Replied by holch on topic Extracting part of string from the answer for validation
So this seems to be a closed question, if you want to compare it to q1.shown, right? So you actually already know what is written in this specific answer, right?
XXX are placeholders or would this really be XXX and are all answers structured the same?
XXX are placeholders or would this really be XXX and are all answers structured the same?
I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.
The topic has been locked.
- arg0naut91
- Topic Author
- Offline
- New Member
Less
More
- Posts: 5
- Thank you received: 0
4 years 7 months ago - 4 years 7 months ago #187248
by arg0naut91
Replied by arg0naut91 on topic Extracting part of string from the answer for validation
Indeed, it is a multiple choice question with pre-defined answers like XXX - Y99, DDD - Y10, EEE - Y88,...
These are all placeholders of course, but the structure is similar.
However due to the fact that in TOKEN:ATTRIBUTE_n I only have strings like ("Y99", "Y10", "Y88"), I cannot do a direct comparison in the sense of {q1.shown} == TOKEN:ATTRIBUTE_n.
These are all placeholders of course, but the structure is similar.
However due to the fact that in TOKEN:ATTRIBUTE_n I only have strings like ("Y99", "Y10", "Y88"), I cannot do a direct comparison in the sense of {q1.shown} == TOKEN:ATTRIBUTE_n.
Last edit: 4 years 7 months ago by arg0naut91.
The topic has been locked.
- DenisChenu
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 13935
- Thank you received: 2551
4 years 7 months ago #187251
by DenisChenu
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
Replied by DenisChenu on topic Extracting part of string from the answer for validation
Did you try {substr(q1.shown,-3) == TOKEN:ATTRIBUTE_n} ?
See manual.limesurvey.org/Expression_Manager#Access_to_Functions
and www.php.net/manual/en/function.substr.php
See manual.limesurvey.org/Expression_Manager#Access_to_Functions
and www.php.net/manual/en/function.substr.php
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The following user(s) said Thank You: gabrieljenik
The topic has been locked.
- arg0naut91
- Topic Author
- Offline
- New Member
Less
More
- Posts: 5
- Thank you received: 0
4 years 7 months ago #187260
by arg0naut91
Replied by arg0naut91 on topic Extracting part of string from the answer for validation
Unfortunately this wouldn't work as the Y* part can be of different length; e.g. it can also be 'Y1', etc.
I'd need to be able to extract it regardless of length - likely with regex.
I'd need to be able to extract it regardless of length - likely with regex.
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10635
- Thank you received: 3698
4 years 7 months ago - 4 years 7 months ago #187262
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Extracting part of string from the answer for validation
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 4 years 7 months ago by tpartner.
The following user(s) said Thank You: DenisChenu, arg0naut91
The topic has been locked.
- Joffm
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 12791
- Thank you received: 3941
4 years 7 months ago #187263
by Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Replied by Joffm on topic Extracting part of string from the answer for validation
Then you have to do it in two steps.
First search search the index of "-" with function "strpos"
then you extract the part after "-" by "substr" and "trim".
E.g.
{trim(substr(q1.shown,strpos(q1.shown,"-")+1))}
{trim(substr("XXX - Y99",strpos("XXX - Y99","-")+1))} --> Y99
{trim(substr("XXX - Y9876",strpos("XXX - Y9876","-")+1))} --> Y9876
{trim(substr("XXX-V1",strpos("XXX-V1","-")+1))} --> Y1
Joffm
First search search the index of "-" with function "strpos"
then you extract the part after "-" by "substr" and "trim".
E.g.
{trim(substr(q1.shown,strpos(q1.shown,"-")+1))}
{trim(substr("XXX - Y99",strpos("XXX - Y99","-")+1))} --> Y99
{trim(substr("XXX - Y9876",strpos("XXX - Y9876","-")+1))} --> Y9876
{trim(substr("XXX-V1",strpos("XXX-V1","-")+1))} --> Y1
Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: DenisChenu, arg0naut91
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10635
- Thank you received: 3698
4 years 7 months ago - 4 years 7 months ago #187264
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Extracting part of string from the answer for validation
Isn't that what I said? (except I am accounting for spaces in " - ")
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 4 years 7 months ago by tpartner.
The topic has been locked.
- Joffm
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 12791
- Thank you received: 3941
4 years 7 months ago #187265
by Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Replied by Joffm on topic Extracting part of string from the answer for validation
@tpartner
So we sent it at the same time.
You sent while I was writing and trying.
Joffm
So we sent it at the same time.
You sent while I was writing and trying.
Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
- arg0naut91
- Topic Author
- Offline
- New Member
Less
More
- Posts: 5
- Thank you received: 0
4 years 7 months ago - 4 years 7 months ago #187288
by arg0naut91
Replied by arg0naut91 on topic Extracting part of string from the answer for validation
Thanks @tpartner and @Joffm - this works!
Last edit: 4 years 7 months ago by arg0naut91.
The topic has been locked.