Hướng dẫn regex find special characters python - regex tìm ký tự đặc biệt python

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]> hello

Tô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

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc
    Examples : 
     

    Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted

    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


    Làm thế nào để tôi tìm thấy các nhân vật đặc biệt trong Python?

    Phương thức: Để kiểm tra xem một ký tự đặc biệt có mặt trong một chuỗi nhất định hay không, trước tiên nhóm tất cả các ký tự đặc biệt là một tập hợp. Sau đó sử dụng cho vòng lặp và nếu các câu lệnh kiểm tra các ký tự đặc biệt.Nếu bất kỳ ký tự đặc biệt nào được tìm thấy thì hãy tăng giá trị của c.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.

    Làm thế nào để bạn tìm thấy các nhân vật đặc biệt?

    Để kiểm tra xem một chuỗi có chứa các ký tự đặc biệt không, hãy gọi phương thức Test () trên biểu thức chính quy phù hợp với bất kỳ ký tự đặc biệt nào.Phương thức kiểm tra sẽ trả về true nếu chuỗi chứa ít nhất 1 ký tự đặc biệt và sai.call the test() method on a regular expression that matches any special character. The test method will return true if the string contains at least 1 special character and false otherwise.

    Là gì?: Trong regex?

    Nó chỉ ra rằng Subpattern là một tiểu đơn vị không bắt giữ.Điều đó có nghĩa là bất cứ điều gì được khớp trong (?indicates that the subpattern is a non-capture subpattern. That means whatever is matched in (?:\w+\s) , even though it's enclosed by () it won't appear in the list of matches, only (\w+) will.

    Làm thế nào để bạn thoát khỏi các nhân vật đặc biệt trong Regex Python?

    Thoát ra với \ để phù hợp với các metacharacters theo nghĩa đen, tức là để loại bỏ ý nghĩa đặc biệt của chúng, tiền tố các ký tự đó có ký tự \ (dấu gạch chéo ngược).Để chỉ ra một ký tự \ theo nghĩa đen, hãy sử dụng \\.prefix those characters with a \ (backslash) character. To indicate a literal \ character, use \\ .

    Chủ đề