Tôi đang trích xuất một chuỗi và cần kiểm tra xem nó có tuân theo một mẫu cụ thể không
<![any word]>
Nếu vậy tôi cần thay thế nó bằng "". Tôi đang thử mã sau
string1 = "<![if support]> hello" string = re.sub(re.compile("<![.*?]>"),"",string1) print(string)Nhưng tôi đang nhận được đầu ra là
<![if support]> helloTôi muốn có được đầu ra như xin chào. Tôi làm gì sai ở đây?
cs95
347K87 Huy hiệu vàng638 Huy hiệu bạc685 Huy hiệu Đồng87 gold badges638 silver badges685 bronze badges
hỏi ngày 26 tháng 7 năm 2017 lúc 11:41Jul 26, 2017 at 11:41
0
[ và ] được coi là ký tự meta trong regex. Bạn sẽ cần phải thoát khỏi chúng:
In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello'Là một sự đơn giản hóa (lịch sự Wiktor Stribiżew), bạn có thể thoát khỏi paren đầu trái đầu tiên, rút ngắn Regex của bạn thành "<!\[.*?]>".
Đã trả lời ngày 26 tháng 7 năm 2017 lúc 11:43Jul 26, 2017 at 11:43
cs95cs95cs95
347K87 Huy hiệu vàng638 Huy hiệu bạc685 Huy hiệu Đồng87 gold badges638 silver badges685 bronze badges
1
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Examples :
Bàn luận Make a regular expression(regex) object of all the special characters that we don’t want, then pass a string in search method. If any one character of string is matching with regex object then search method returns a match object otherwise return None.
Below is the implementation :
C++
<![if support]> hello 0
<![if support]> hello 1
Đưa ra một chuỗi, tác vụ là kiểm tra xem chuỗi đó có chứa bất kỳ ký tự đặc biệt nào không (bộ ký tự đặc biệt được xác định). Nếu bất kỳ ký tự đặc biệt nào được tìm thấy, don don chấp nhận chuỗi.Examples đó: & nbsp; & nbsp;
Cách tiếp cận: Tạo đối tượng biểu thức thông thường (Regex) của tất cả các ký tự đặc biệt mà chúng tôi không muốn, sau đó chuyển một chuỗi trong phương thức tìm kiếm. Nếu bất kỳ một ký tự nào của chuỗi phù hợp với đối tượng Regex thì phương thức tìm kiếm sẽ trả về một đối tượng khớp nếu không trả về không.
<![if support]> hello 7
<![if support]> hello 8<![if support]> hello 9In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 0In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 1
<![if support]> hello 8In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 3In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 4
In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 5In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 6In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 7In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 8
<![if support]> hello 8Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted0
In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 5In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 6Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted3In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 8
Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted5
<![if support]> hello 2 <![if support]> hello 3 <![if support]> hello 4
<![if support]> hello 7
<![if support]> hello 8String is not accepted.0String is not accepted.1In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 8
<![if support]> hello 8String is not accepted.4
<![if support]> hello 5 <![if support]> hello 6
Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted5
Python3
Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted6 Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted7
<![if support]> hello 8String is not accepted.6 String is not accepted.7
String is not accepted.9 string is not accepted0
string is not accepted1 string is not accepted2
In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 5<![any word]>9string is not accepted8In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 7<![any word]>0
<![if support]> hello 8Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted0[5
In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 5<![any word]>9string is not accepted8Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted3<![any word]>0
<![if support]> hello 8string is not accepted4string is not accepted5 string is not accepted6string is not accepted7string is not accepted8string is not accepted9<![any word]>0
Các
<![if support]> hello 8"<!\[.*?]>"2
In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 3 ]2string is not accepted55____55 ]5 [5
"<!\[.*?]>"3
<![if support]> hello 8]8string is not accepted5 String is not accepted.1
<![if support]> hello 7
PHP
<![if support]> hello 04"<!\[.*?]>"6In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 1
<![if support]> hello 8In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 3string is not accepted8<![if support]> hello 00<![any word]>0
In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 5<![any word]>9string is not accepted8In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 7In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 1
<![if support]> hello 8Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted0
In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 5<![any word]>9string is not accepted8Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted3In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 1
Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted5
"<!\[.*?]>"4 "<!\[.*?]>"5"<!\[.*?]>"6<![any word]>0
"<!\[.*?]>"5"<!\[.*?]>"6In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 1
<![if support]> hello 32
<![if support]> hello 8<![if support]> hello 00 <![if support]> hello 01string is not accepted9<![if support]> hello 03
String is not accepted."<!\[.*?]>"6 <![if support]> hello 26<![if support]> hello 27In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 8 To check if a special character is present in a given string or not, firstly group all special characters as one set. Then using for loop and if statements check for special characters. If any special character is found then increment the value of c. Finally, check if the c value is greater than zero then print string is not accepted otherwise print string is accepted.
C++
<![if support]> hello 0
<![if support]> hello 34
Đưa ra một chuỗi, tác vụ là kiểm tra xem chuỗi đó có chứa bất kỳ ký tự đặc biệt nào không (bộ ký tự đặc biệt được xác định). Nếu bất kỳ ký tự đặc biệt nào được tìm thấy, don don chấp nhận chuỗi.Examples đó: & nbsp; & nbsp;
Cách tiếp cận: Tạo đối tượng biểu thức thông thường (Regex) của tất cả các ký tự đặc biệt mà chúng tôi không muốn, sau đó chuyển một chuỗi trong phương thức tìm kiếm. Nếu bất kỳ một ký tự nào của chuỗi phù hợp với đối tượng Regex thì phương thức tìm kiếm sẽ trả về một đối tượng khớp nếu không trả về không.
<![if support]> hello 2 <![if support]> hello 3 <![if support]> hello 4
<![if support]> hello 5 <![if support]> hello 6
<![if support]> hello 40<![if support]> hello 48<![if support]> hello 49<![if support]> hello 50
Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted6 Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted7
<![if support]> hello 40<![if support]> hello 7
<![if support]> hello 8String is not accepted.6 String is not accepted.7
<![if support]> hello 8<![if support]> hello 7
<![if support]> hello 63<![if support]> hello 64
<![if support]> hello 8Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted5
<![if support]> hello 40Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted5
<![if support]> hello 40In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 3<![if support]> hello 71
String is not accepted.9 string is not accepted0
<![if support]> hello 40Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted5
<![if support]> hello 40Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted0<![if support]> hello 7
<![if support]> hello 8In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 6<![if support]> hello 83In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 8
<![if support]> hello 40Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted5
<![if support]> hello 5 <![if support]> hello 6
Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted6 Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted7
Python3
<![if support]> hello 91string is not accepted5String is not accepted.1
<![if support]> hello 94
<![if support]> hello 95string is not accepted5<![if support]> hello 97
<![if support]> hello 8String is not accepted.6 String is not accepted.7
String is not accepted.9 string is not accepted0
string is not accepted1 string is not accepted2
<![if support]> hello 8string is not accepted4string is not accepted5 string is not accepted6string is not accepted7string is not accepted8string is not accepted9<![any word]>0
Các
<![if support]> hello 8<![any word]>9string is not accepted8<![if support]> hello 74<![any word]>0
Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted0[5
<![if support]> hello 8<![any word]>9string is not accepted8In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello") Out[1]: ' hello' 32<![any word]>0
<![if support]> hello 8<![if support]> hello 00 <![if support]> hello 01string is not accepted9<![if support]> hello 03
string is not accepted