概述
To solve this problem easily, we will use the re module in the program. A string will be given by the user and we have to extract the mobile number by using the Python programming language. Before going to use the re module, we will learn a little bit about re module.
为了轻松解决此问题,我们将在程序中使用re模块 。 用户将给出一个字符串,我们必须使用Python编程语言来提取手机号码。 在使用re模块之前 ,我们将学习有关re模块的一些知识 。
Python has an inbuilt re module which allows us to solve the various problems based on pattern matching and string manipulation. The re module or regex is an abbreviated form of the regular expression. This module of Python in our daily life is very useful, by using re module you can find the contact number, email-id, special pattern, etc from a file or given sentences. The re module provides a lot of metacharacters and to solve the problem we will use the d which is used to match any decimal digits(0-9) in the given string or sentence.
Python有一个内置的re模块 ,它使我们能够解决基于模式匹配和字符串操作的各种问题。 re模块或regex是正则表达式的缩写形式。 Python的这个模块在我们的日常生活中非常有用,通过使用re模块,您可以从文件或给定的句子中找到联系电话,电子邮件ID,特殊模式等。 re模块提供了许多元字符,为了解决该问题,我们将使用 d来匹配给定字符串或句子中的任何十进制数字(0-9)。
Let's take an example to understand the problem better,
让我们举个例子来更好地理解问题,
Input:
"Hello! I am bipin Kumar and my contact number is 918481234422.
May i know the call logs of my number"
Output:
918481234422
Explanation:
The program will have to find a substring that contains
12 digit substring and print it.
Algorithm to solve the above problem
解决以上问题的算法
Initially, we will import the re module in the program.
最初,我们将在程序中导入re模块 。
Assign the given string or sentence in a variable.
在变量中分配给定的字符串或句子。
As we all know the contact number or mobile will be of 12 digits that why we will form a format that will use to extract the mobile number.
众所周知,联系电话或手机号码为12位数字,这就是为什么我们将形成一种用于提取手机号码的格式。
Now, print the extracted number.
现在,打印提取的号码。
Program:
程序:
# importing the module
import re
# string
string='''If you would like to get in touch with us through other ways,
the Flipkart customer support number is 018002089898.
And we're just a call away if you need anything.
You can also arrange a call-back from within the
Flipkart app regarding any issue related to your order.'''
# extracting the mobile number
Phonenumber=re.compile(r'dddddddddddd')
m=Phonenumber.search(string)
# printing the result
print('mobile number found from the string : ',m.group())
Output
输出量
mobile number found from the string :
018002089898
翻译自: https://www.includehelp.com/python/extract-the-mobile-number-from-the-given-string.aspx
最后
以上就是高贵服饰为你收集整理的从Python中给定的字符串中提取手机号码的全部内容,希望文章能够帮你解决从Python中给定的字符串中提取手机号码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复