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?

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

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

[] đượ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

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

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 
    
    9
    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    0
    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    1

    <![if support]> hello 
    
    8
    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    3
    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    4

    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    5
    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    6
    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    7
    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    8

    <![if support]> hello 
    
    8
    Input : Geeks$For$Geeks
    Output : String is not accepted.
    
    Input : Geeks For Geeks
    Output : String is accepted
    0

    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    5
    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    6
    Input : Geeks$For$Geeks
    Output : String is not accepted.
    
    Input : Geeks For Geeks
    Output : String is accepted
    3
    In [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 accepted
    5

    <![if support]> hello 
    
    2
    <![if support]> hello 
    
    3
    <![if support]> hello 
    
    4

    <![if support]> hello 
    
    7

    <![if support]> hello 
    
    8
    String is not accepted.
    0
    String is not accepted.
    1
    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    8

    <![if support]> hello 
    
    8
    String 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 accepted
    5

    Python3

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

    <![if support]> hello 
    
    8
    String is not accepted.
    6
    String is not accepted.
    7

    String is not accepted.
    9
    string is not accepted
    0

    string is not accepted
    1
    string is not accepted
    2

    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    5<![any word]>9
    string is not accepted
    8
    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    7<![any word]>0

    <![if support]> hello 
    
    8
    Input : Geeks$For$Geeks
    Output : String is not accepted.
    
    Input : Geeks For Geeks
    Output : String is accepted
    0[5

    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    5<![any word]>9
    string is not accepted
    8
    Input : Geeks$For$Geeks
    Output : String is not accepted.
    
    Input : Geeks For Geeks
    Output : String is accepted
    3<![any word]>0

    <![if support]> hello 
    
    8
    string is not accepted
    4
    string is not accepted
    5
    string is not accepted
    6
    string is not accepted
    7
    string is not accepted
    8
    string is not accepted
    9<![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]8
    string is not accepted
    5
    String is not accepted.
    1

    <![if support]> hello 
    
    7

    PHP

    <![if support]> hello 
    
    04"<!\[.*?]>"6
    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    1

    <![if support]> hello 
    
    8
    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    3
    string is not accepted
    8
    <![if support]> hello 
    
    00<![any word]>0

    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    5<![any word]>9
    string is not accepted
    8
    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    7
    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    1

    <![if support]> hello 
    
    8
    Input : Geeks$For$Geeks
    Output : String is not accepted.
    
    Input : Geeks For Geeks
    Output : String is accepted
    0

    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    5<![any word]>9
    string is not accepted
    8
    Input : Geeks$For$Geeks
    Output : String is not accepted.
    
    Input : Geeks For Geeks
    Output : String is accepted
    3
    In [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 accepted
    5

    "<!\[.*?]>"4 "<!\[.*?]>"5"<!\[.*?]>"6<![any word]>0

    "<!\[.*?]>"5"<!\[.*?]>"6

    In [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 
    
    01
    string is not accepted
    9
    <![if support]> hello 
    
    03

    String is not accepted.

    "<!\[.*?]>"6

    <![if support]> hello 
    
    26
    <![if support]> hello 
    
    27
    In [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 accepted
    6
    Input : Geeks$For$Geeks
    Output : String is not accepted.
    
    Input : Geeks For Geeks
    Output : String is accepted
    7

    <![if support]> hello 
    
    40
    <![if support]> hello 
    
    7

    <![if support]> hello 
    
    8
    String 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 
    
    8
    Input : Geeks$For$Geeks
    Output : String is not accepted.
    
    Input : Geeks For Geeks
    Output : String is accepted
    5

    <![if support]> hello 
    
    40
    Input : Geeks$For$Geeks
    Output : String is not accepted.
    
    Input : Geeks For Geeks
    Output : String is accepted
    5

    <![if support]> hello 
    
    40
    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    3
    <![if support]> hello 
    
    71

    String is not accepted.
    9
    string is not accepted
    0

    <![if support]> hello 
    
    40
    Input : Geeks$For$Geeks
    Output : String is not accepted.
    
    Input : Geeks For Geeks
    Output : String is accepted
    5

    <![if support]> hello 
    
    40
    Input : Geeks$For$Geeks
    Output : String is not accepted.
    
    Input : Geeks For Geeks
    Output : String is accepted
    0
    <![if support]> hello 
    
    7

    <![if support]> hello 
    
    8
    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    6
    <![if support]> hello 
    
    83
    In [1]: re.sub(re.compile("<!\[.*?\]>"), "", "<![if support]> hello")
    Out[1]: ' hello'
    
    8

    <![if support]> hello 
    
    40
    Input : Geeks$For$Geeks
    Output : String is not accepted.
    
    Input : Geeks For Geeks
    Output : String is accepted
    5

    <![if support]> hello 
    
    5
    <![if support]> hello 
    
    6

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

    Python3

    <![if support]> hello 
    
    91
    string is not accepted
    5
    String is not accepted.
    1

    <![if support]> hello 
    
    94

    <![if support]> hello 
    
    95
    string is not accepted
    5
    <![if support]> hello 
    
    97

    <![if support]> hello 
    
    8
    String is not accepted.
    6
    String is not accepted.
    7

    String is not accepted.
    9
    string is not accepted
    0

    string is not accepted
    1
    string is not accepted
    2

    <![if support]> hello 
    
    8
    string is not accepted
    4
    string is not accepted
    5
    string is not accepted
    6
    string is not accepted
    7
    string is not accepted
    8
    string is not accepted
    9<![any word]>0

    Các

    <![if support]> hello 
    
    8<![any word]>9
    string is not accepted
    8
    <![if support]> hello 
    
    74<![any word]>0

    Input : Geeks$For$Geeks
    Output : String is not accepted.
    
    Input : Geeks For Geeks
    Output : String is accepted
    0[5

    <![if support]> hello 
    
    8<![any word]>9
    string is not accepted
    8
    In [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 
    
    01
    string is not accepted
    9
    <![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 \\ .