试图匹配一个字符串 [英] trying to match a string
问题描述
我正在把一个字符串作为用户的输入,它应该只有
包含字符:L,M或R
我在kodos中试过以下但它们仍然不完美:
[^ AK,^ NQ,^ SZ,^ 0-9]
[L] [M] [R]
[LRM]?L?[LRM]?等等,但它们并不完全符合我的需要。
例如:LRLRLRLRLM没问题,但LRLRLRNL并不像N那样。就像那样。
< br $> b $ b问候,
SZ
字符串可能有也可能没有全部三个字符。
Hi,
Hi,
I am taking a string as an input from the user and it should only
contain the chars:L , M or R
I tried the folllowing in kodos but they are still not perfect:
[^A-K,^N-Q,^S-Z,^0-9]
[L][M][R]
[LRM]?L?[LRM]? etc but they do not exactly meet what I need.
For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has ''N'' .like that.
regards,
SZ
The string may or may not have all the three chars.
推荐答案
7月18日晚上8:33,arnimavidyar ... @ gmail.com写道:
On Jul 18, 8:33 pm, arnimavidyar...@gmail.com wrote:
我从一个字符串作为用户的输入,它应该只包含
字符:L,M或者R
我尝试了下面的kodos,但它们仍然不完美:
[^ AK,^ NQ,^ SZ,^ 0-9]
[L] [M] [R]
[LRM]?L?[LRM]?等等,但它们并不完全符合我的需要。
例如:LRLRLRLRLM没问题,但LRLRLRNL并不像N那样。就像那样。
问候,
SZ
字符串可能有也可能没有全部三个字符。
Hi,
Hi,
I am taking a string as an input from the user and it should only
contain the chars:L , M or R
I tried the folllowing in kodos but they are still not perfect:
[^A-K,^N-Q,^S-Z,^0-9]
[L][M][R]
[LRM]?L?[LRM]? etc but they do not exactly meet what I need.
For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has ''N'' .like that.
regards,
SZ
The string may or may not have all the three chars.
re.match(r''[LMR] + \''',your_string)
英文: L,M或R中的一个或多个,后跟
字符串的结尾。
re.match(r''[LMR]+\Z'', your_string)
in English: one or more of L, M , or R, followed by the end of the
string.
7月18日,11:33 * am,arnimavidyar ... @ gmail.com写道:
On Jul 18, 11:33*am, arnimavidyar...@gmail.com wrote:
我从一个字符串作为用户的输入,它应该只有
包含字符:L,M或R
我尝试了下面的kodos,但它们仍然不完美:
[^ AK,^ NQ,^ SZ,^ 0-9]
[L ] [M] [R]
[LRM]?L?[LRM]?等等,但它们并不完全符合我的需要。
例如:LRLRLRLRLM没问题,但LRLRLRNL并不像N那样。就像那样。
问候,
SZ
字符串可能有也可能没有全部三个字符。
Hi,
Hi,
I am taking a string as an input from the user and it should only
contain the chars:L , M or R
I tried the folllowing in kodos but they are still not perfect:
[^A-K,^N-Q,^S-Z,^0-9]
[L][M][R]
[LRM]?L?[LRM]? etc but they do not exactly meet what I need.
For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has ''N'' .like that.
regards,
SZ
The string may or may not have all the three chars.
使用正则表达式,[^ LRM]匹配一个不是L的字符,R
或M.所以:
导入重新
var =" LRLRLRLNR"
如果re.search(r'' [^ LRM]'',var):
print" Invalid"
With regular expressions, [^LRM] matches a character that isn''t L, R
or M. So:
import re
var = "LRLRLRLNR"
if re.search(r''[^LRM]'', var):
print "Invalid"
7月18日9:05 pm,oj< ojee ... @ gmail.comwrote:
On Jul 18, 9:05 pm, oj <ojee...@gmail.comwrote:
7月18日上午11点33分,arnimavidyar ... @ gmail.com写道:
On Jul 18, 11:33 am, arnimavidyar...@gmail.com wrote:
Hi,
Hi,
我从一个字符串作为用户的输入,它应该只有
包含字符:L,M或R
I am taking a string as an input from the user and it should only
contain the chars:L , M or R
我尝试了下面的kodos,但它们仍然不完美:
I tried the folllowing in kodos but they are still not perfect:
[^ AK,^ NQ,^ SZ,^ 0-9]
[L ] [M] [R]
[LRM]?L?[LRM]?等,但他们并不完全符合我的需要。
[^A-K,^N-Q,^S-Z,^0-9]
[L][M][R]
[LRM]?L?[LRM]? etc but they do not exactly meet what I need.
例如:LRLRLRLRLM没问题,但LRLRLRNL不像'N'那样。
For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has ''N'' .like that.
问候,
SZ
regards,
SZ
字符串可能包含也可能不包含所有三个字符。
The string may or may not have all the three chars.
使用正则表达式,[^ LRM]匹配一个不是L的字符,R
或M.所以:
导入重新
var =" LRLRLRLNR"
如果re.search(r'' [^ LRM]'',var):
print" Invalid"
With regular expressions, [^LRM] matches a character that isn''t L, R
or M. So:
import re
var = "LRLRLRLNR"
if re.search(r''[^LRM]'', var):
print "Invalid"
如果var引用空字符串,则失败。
Fails if var refers to the empty string.
这篇关于试图匹配一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!