MATLAB: How to restrict input string in length,symbols, char and numbers

MATLAB

I am developing an app where the user inputs a string which need several restrictions. How can I check that the input is in the format 'LLL-NN-NNN' where L is lettes (A-Z) and N is numbers (0-9), and the string have to be exactly 10 characters in total.

Best Answer

>> str = 'XYZ-99-123'; % okay
>> ~isempty(regexp(str,'^[A-Z]{3}-\d{2}-\d{3}$','once'))
ans = 1
>> str = 'XYZ-99-ABC'; % not okay


>> ~isempty(regexp(str,'^[A-Z]{3}-\d{2}-\d{3}$','once'))
ans = 0
>> str = '666-99-123'; % not okay
>> ~isempty(regexp(str,'^[A-Z]{3}-\d{2}-\d{3}$','once'))
ans = 0
>> str = 'hello world'; % not okay
>> ~isempty(regexp(str,'^[A-Z]{3}-\d{2}-\d{3}$','once'))
ans = 0
Use that logical output to either throw an error, or ask the user to supply new input data, or whatever suits your process.
Related Question