S
S
stikname2014-01-05 12:07:47
PHP
stikname, 2014-01-05 12:07:47

regexp in php. Checking a string for the presence of the desired sequences?

There is a string consisting of sequences matching this regular expression:
%u00[0-9][0-9a-zA-Z]
For example:

%u003c%u0073%u0063%u0072%u0069%u0070%u0074%u003e

We need a regular expression, in php, which would check whether the line is written correctly.
Correct line options (one per line):
%u003c%u0073%u0063%u0072%u0069%u0070%u0074%u003e
%u003c
%u0046%u005s

Incorrect line options (one per line):
asd
%a003da
%k41ad
blah blah blah
%u003c%u0073%u0063%
%u003c%u0073ASFQ%u0063

I don't know how to express my idea better.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
silentvick, 2014-01-05
@stikname

If we take it for granted that we are talking about escaped unicode, where %u starts the character code, followed by a hexadecimal representation, then the following regular expression will do:
/^(?:%u[\dabcdef]{4})+$/i

M
Melkij, 2014-01-05
@melkij

In your third positive test, c is Russian, but I think why the test is failing =)

function checkStr( $sInput ) {
  $sRegexp = '~^(:?%u00[0-9][0-9a-zA-Z])++$~';
  return preg_match($sRegexp, $sInput);
  }
assert('checkStr("%u003c%u0073%u0063%u0072%u0069%u0070%u0074%u003e")');
assert('checkStr("%u003c")');
assert('checkStr("%u0046%u005c")');
assert('checkStr("asd")==false');
assert('checkStr("%a003da")==false');
assert('checkStr("%k41ad")==false');
assert('checkStr("Бла бла бла")==false');
assert('checkStr("%u003c%u0073%u0063%")==false');
assert('checkStr("%u003c%u0073ASFQ%u0063")==false');

A
avalak, 2014-01-05
@avalak

/^(?:%u00[\da-z]{2})+$/
test

R
Rsa97, 2014-01-05
@Rsa97

If the empty string is not the correct result, then
If is, then

preg_match('/^(%u00[0-9][0-9a-zA-Z])*$/', $string);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question