Regular expression to find bar

Asked

Viewed 3,695 times

4

I need to validate a field and it must have the following format: two letters/numbers. Ex.: RN/1234567. The two letters will always be uppercase and the number quantity has no limit.

  • I think it would be something like /[A-Z]{2}\/[0-9]{1,}/.

  • Will the two numbers always be uppercase? Better edit this for two letters or you won’t get the right answer :)

5 answers

5


I’m not very good at Regex, but I think this one’s for you:

[A-Z]{2}[\/][0-9]{1,}

Explanation:

  • [A-Z]{2}:Find two characters from A to Z
  • [\/]: Find a bar
  • [1-9]{1,}: Finds from 1 to infinity numbers
  • Thank you for the explanation, my dear.

  • 2

    In his Regex zero was excluded?

  • @Leocaracciolo thanks for the remark

4

In general, if you want to validate if there are two letters, uppercase, followed by a bar and then only numbers, no specific size you can use:

[A-Z]{2}\/[0-9]{1,}

This will make it valid for doos characters between A and Z. Then a slider, escaped, to require the slider to exist after both letters. Then check for numbers, between 0 and 9, at least there must be a number to infinity.

  • Thank you for the explanation, my dear.

  • Even if it was via comment you were the first to reply. Up given

2

A solution: str.match(/\w{2}\/\d+/g)

  • \w{2} any letter (wOrd) 2 times
  • \/ a bar /
  • \d+ any digit (digit) 1 or more times

2

To complement the above answers, to use the regular expression explained above you can do as follows:

PHP

$codigo = 'RN/1234567';

$regex = '~[A-Z]{2}\/[1-9]{1,}~';

if (preg_match($regex, $codigo)) {
    echo 'Código válido!';
}  else {
     echo 'Insira um código válido!';
}

Javascript

var codigo = 'RN/1234560';
var regex = /[A-Z]{2}\/[1-9]{1,}/g;
if (regex.test(codigo)) {
    console.log('Código válido!');
} else {
    console.log('Insira um código válido!');
}

1

With Javascript, you can do so:

function validarMeuCampo(texto) {
    var captura = texto.match(/[A-Z][A-Z]\/[0-9]+/);
    var valido = !!captura && captura.length == 1 && captura[0].length == texto.length;
    return valido;
}

Explanation:

The regular expression /[A-Z][A-Z]\/[0-9]+/ captures two uppercase letters, followed by a bar, followed by a number of numbers. The number amount is unlimited, but there must be at least one number (that’s what the + to the right of the numbers makes).

The method match of the String type takes a regular expression and returns an array of portions of the String to which the expression found something. If you find nothing, returns null.

The operator ! does a negation. It happens that in Javascript all is deniable. Denying twice is a way of finding out whether a variable that should be an object has value, because the negation of an object is false, and the negation of null is true. In other words: the opposite of my opposite is a boolean value that tells me whether I have value or not.

Putting it all together now: find in the given text snippets that match the regular expression. If you find a single result that matches the string, then the string is valid for your rule.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.