Hướng dẫn regular expression to extract number from string python - biểu thức chính quy để trích xuất số từ chuỗi python

Điều này còn hơn một chút, nhưng bạn có thể mở rộng biểu thức regex để tính toán ký hiệu khoa học quá.

import re

# Format is [(<string>, <expected output>), ...]
ss = [("apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
       ['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']),
      ('hello X42 I\'m a Y-32.35 string Z30',
       ['42', '-32.35', '30']),
      ('he33llo 42 I\'m a 32 string -30', 
       ['33', '42', '32', '-30']),
      ('h3110 23 cat 444.4 rabbit 11 2 dog', 
       ['3110', '23', '444.4', '11', '2']),
      ('hello 12 hi 89', 
       ['12', '89']),
      ('4', 
       ['4']),
      ('I like 74,600 commas not,500', 
       ['74,600', '500']),
      ('I like bad math 1+2=.001', 
       ['1', '+2', '.001'])]

for s, r in ss:
    rr = re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", s)
    if rr == r:
        print('GOOD')
    else:
        print('WRONG', rr, 'should be', r)

Cho tất cả tốt!

Ngoài ra, bạn có thể nhìn vào Regex tích hợp keo AWS

Làm thế nào để chúng tôi trích xuất các giá trị từ danh sách ?.

  • .
  • .
  • .
  • Nội dung

Giới thiệu

Ví dụ 1: Lấy danh sách tất cả các số trong một chuỗi‘[0-9]+’ with re.findall() method. [0-9] represents a regular expression to match a single digit in the string. [0-9]+ represents continuous digit sequences of any length.

numbers = re.findall('[0-9]+', str)

Ví dụ 2: Lấy danh sách tất cả các chữ số liên tục trong một chuỗistr is the string in which we need to find the numbers. re.findall() returns list of strings that are matched with the regular expression.

.

.

Nội dung

import re

str = 'We live at 9-162 Malibeu. My phone number is 666688888.'
#search using regex
x = re.findall('[0-9]+', str)
print(x)

Giới thiệu

Ví dụ 1: Lấy danh sách tất cả các số trong một chuỗi

['9', '162', '666688888']

.

Nội dung

Nội dung

import re

str = 'We four guys, live at 2nd street of Malibeu. I had a cash of $248 in my pocket. I got a ticket with serial number 88796451-52.'
#search using regex
x = re.findall('[0-9]+', str)
print(x)

Giới thiệu

Ví dụ 1: Lấy danh sách tất cả các số trong một chuỗi

['2', '248', '88796451', '52']

Nội dung

Giới thiệu

Trên khung của công cụ trích xuất, chọn nút Radio Số trích xuất ..

Làm thế nào để bạn trích xuất các số từ một danh sách trong Python?

  • Làm thế nào để chúng tôi trích xuất các giá trị từ danh sách ?.
  • .
  • Trên khung của công cụ trích xuất, chọn nút Radio Số trích xuất ..

    Làm thế nào để bạn trích xuất các số từ một danh sách trong Python?

    Làm thế nào để chúng tôi trích xuất các giá trị từ danh sách ?.

    .
    This problem can be solved by using split function to convert string to list and then the list comprehension which can help us iterating through the list and isdigit function helps to get the digit out of a string.

    numbers = re.findall('[0-9]+', str)
    4
    numbers = re.findall('[0-9]+', str)
    5
    numbers = re.findall('[0-9]+', str)
    6

    numbers = re.findall('[0-9]+', str)
    7
    numbers = re.findall('[0-9]+', str)
    8
    numbers = re.findall('[0-9]+', str)
    9
    import re
    
    str = 'We live at 9-162 Malibeu. My phone number is 666688888.'
    #search using regex
    x = re.findall('[0-9]+', str)
    print(x)
    0
    import re
    
    str = 'We live at 9-162 Malibeu. My phone number is 666688888.'
    #search using regex
    x = re.findall('[0-9]+', str)
    print(x)
    1

    import re
    
    str = 'We live at 9-162 Malibeu. My phone number is 666688888.'
    #search using regex
    x = re.findall('[0-9]+', str)
    print(x)
    2
    numbers = re.findall('[0-9]+', str)
    5
    import re
    
    str = 'We live at 9-162 Malibeu. My phone number is 666688888.'
    #search using regex
    x = re.findall('[0-9]+', str)
    print(x)
    4
    import re
    
    str = 'We live at 9-162 Malibeu. My phone number is 666688888.'
    #search using regex
    x = re.findall('[0-9]+', str)
    print(x)
    5
    import re
    
    str = 'We live at 9-162 Malibeu. My phone number is 666688888.'
    #search using regex
    x = re.findall('[0-9]+', str)
    print(x)
    6
    import re
    
    str = 'We live at 9-162 Malibeu. My phone number is 666688888.'
    #search using regex
    x = re.findall('[0-9]+', str)
    print(x)
    7
    import re
    
    str = 'We live at 9-162 Malibeu. My phone number is 666688888.'
    #search using regex
    x = re.findall('[0-9]+', str)
    print(x)
    8__

    numbers = re.findall('[0-9]+', str)
    7
    numbers = re.findall('[0-9]+', str)
    8
    ['9', '162', '666688888']
    5
    import re
    
    str = 'We live at 9-162 Malibeu. My phone number is 666688888.'
    #search using regex
    x = re.findall('[0-9]+', str)
    print(x)
    0
    ['9', '162', '666688888']
    7
    ['9', '162', '666688888']
    8

    Đầu ra:

    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    

    Phương pháp số 2: Sử dụng

    ['9', '162', '666688888']
    9 Vấn đề cụ thể này cũng có thể được giải quyết bằng cách sử dụng Python Regex, chúng ta có thể sử dụng hàm Findall để kiểm tra các lần xuất hiện bằng cách sử dụng chuỗi regex phù hợp.
    This particular problem can also be solved using python regex, we can use the findall function to check for the numeric occurrences using matching regex string.

    import re
    
    str = 'We four guys, live at 2nd street of Malibeu. I had a cash of $248 in my pocket. I got a ticket with serial number 88796451-52.'
    #search using regex
    x = re.findall('[0-9]+', str)
    print(x)
    0
    import re
    
    str = 'We four guys, live at 2nd street of Malibeu. I had a cash of $248 in my pocket. I got a ticket with serial number 88796451-52.'
    #search using regex
    x = re.findall('[0-9]+', str)
    print(x)
    1

    numbers = re.findall('[0-9]+', str)
    4
    numbers = re.findall('[0-9]+', str)
    5
    numbers = re.findall('[0-9]+', str)
    6

    numbers = re.findall('[0-9]+', str)
    7
    numbers = re.findall('[0-9]+', str)
    8
    numbers = re.findall('[0-9]+', str)
    9
    import re
    
    str = 'We live at 9-162 Malibeu. My phone number is 666688888.'
    #search using regex
    x = re.findall('[0-9]+', str)
    print(x)
    0
    import re
    
    str = 'We live at 9-162 Malibeu. My phone number is 666688888.'
    #search using regex
    x = re.findall('[0-9]+', str)
    print(x)
    1

    ['2', '248', '88796451', '52']
    0__15
    ['2', '248', '88796451', '52']
    2
    ['2', '248', '88796451', '52']
    3
    ['2', '248', '88796451', '52']
    4

    import re
    
    str = 'We live at 9-162 Malibeu. My phone number is 666688888.'
    #search using regex
    x = re.findall('[0-9]+', str)
    print(x)
    2
    numbers = re.findall('[0-9]+', str)
    5
    ['2', '248', '88796451', '52']
    7
    numbers = re.findall('[0-9]+', str)
    8
    ['2', '248', '88796451', '52']
    9
    numbers = re.findall('[0-9]+', str)
    8
    import re
    
    str = 'We live at 9-162 Malibeu. My phone number is 666688888.'
    #search using regex
    x = re.findall('[0-9]+', str)
    print(x)
    5
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    2

    numbers = re.findall('[0-9]+', str)
    7
    numbers = re.findall('[0-9]+', str)
    8
    ['9', '162', '666688888']
    5
    import re
    
    str = 'We live at 9-162 Malibeu. My phone number is 666688888.'
    #search using regex
    x = re.findall('[0-9]+', str)
    print(x)
    0
    ['9', '162', '666688888']
    7
    ['9', '162', '666688888']
    8

    Đầu ra:

    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    


    Làm cách nào để trích xuất số từ một chuỗi trong Python?

    Vấn đề này có thể được giải quyết bằng cách sử dụng chức năng phân chia để chuyển đổi chuỗi thành danh sách và sau đó danh sách hiểu có thể giúp chúng tôi lặp qua chức năng danh sách và isDigit giúp loại bỏ chữ số ra khỏi chuỗi.using split function to convert string to list and then the list comprehension which can help us iterating through the list and isdigit function helps to get the digit out of a string.

    Làm thế nào để bạn nhận được một số từ một chuỗi trong regex?

    Python Regex - Nhận danh sách tất cả các số từ chuỗi.Để có được danh sách tất cả các số trong một chuỗi, hãy sử dụng biểu thức thông thường '[0-9]+' với phương thức re.findall ().[0-9] biểu thị một biểu thức chính quy để khớp với một chữ số trong chuỗi.use the regular expression '[0-9]+' with re. findall() method. [0-9] represents a regular expression to match a single digit in the string.

    Làm thế nào để bạn trích xuất một số ra khỏi một chuỗi?

    Trích xuất số từ chuỗi văn bản với Ultimate Suite..
    Chuyển đến tab Dữ liệu ABLEBITS> Nhóm văn bản và nhấp vào Trích xuất:.
    Chọn tất cả các ô với chuỗi nguồn ..
    Trên khung của công cụ trích xuất, chọn nút Radio Số trích xuất ..

    Làm thế nào để bạn trích xuất các số từ một danh sách trong Python?

    Làm thế nào để chúng tôi trích xuất các giá trị từ danh sách ?..
    .
    .
    .