Borrar filtros
Borrar filtros

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

4 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Stephen23
Stephen23 el 7 de Mzo. de 2020
Editada: Stephen23 el 7 de Mzo. de 2020
>> 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.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by