Regex - If contains '%', may contain only '% 20'

I want to create a regex for the following scenario:

If the string contains a percentage character ( %), then it can contain only the following: %20and cannot be preceded by another. ' %'

So, if it were, for example %25, it would be rejected. For example, the following line is valid:

http://www.test.com/?&Name=My%20Name%20Is%20Vader

But this will not work:

http://www.test.com/?&Name=My%20Name%20Is%20VadersAccountant%25

%%%25

Any help would be greatly appreciated.

Kyle


EDIT:

The script in a nutshell is that the link is written in an encoded state, and then launched through JavaScript. Decoding operation is not performed. I tried decoding .net and JS decoding, each of which has the same result. Results remain encoded during execution.

+5
9

%:

/^[^%]*(%20[^%]*)*$/
+5

?

Uri Encoder/Decoder. ( ) .

. - /[\ w]/( )

, , , www.example.com/index.html?user=admin&pass=%%250 , "% 250".

+2

, ,

/^([^%]|%%|%20)+$/

: , %% URI
Edit2: , :-)
Edit3:

, ( , ), , , Vim, :

/^\([^%]\|%%\|%20\)\+$/
+1

, :

^([^%]|%([013-9a-fA-F][0-9a-fA-F]|2[1-9a-fA-F]))*$
+1

, %[^2][^0]

+1

, , , :

string name = HttpUtility.UrlDecode(Request.QueryString["Name"]);
0

. Regex.

, %, , 20 . ( : % %nnn)

// pseudo code
pos = 0
while (pos = mystring.find(pos, '%'))
{
     if mystring[pos+1] = "%" then
         pos = pos + 2 // ok, this is a literal, skip ahead
     else if mystring.substring(pos,2) != "20" 
          return false; // string is invalid
     end if
}
return true;
0
/^([^%]|%20)*$/
0

"" . %20 - , .

, %% ... %%25 %25

, .

/(?<![^%]%)%(?!(20|%))/

, % ( %%), , %%%. % , 20

, - , , , .

0

All Articles