匹配邮箱和匹配网页和匹配电话号码

匹配邮箱

import re
mylist = ['aaa@163.com', 'bbb@126.com', 'ccc@qq.com']
for i in mylist:
    match_obj = re.match('\w{3,15}@(163|162)\.com$',i)
    if match_obj:
        print(match_obj.group())
        print(match_obj.group(1))
    else:
        print('没有匹配')

匹配网页

import re
content = '<htm1>hello world!</html>'
obj = re.match('<[a-zA-Z0-9]+>(.*)</[a-zA-Z0-9]+>')
if obj:
    print(obj.group(1))
else:
    print('0')

匹配电话号码

import re
list = ['010-12345','010123456']
for i in list:
    obj = re.match('^0[0-9]{2,3}-?\d{5}',i)
    if obj:
        print(obj.group())
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
北京大学oj题目,已提交AC。原题目如下: Description Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes only part of the number is used to spell a word. When you get back to your hotel tonight you can order a pizza from Gino's by dialing 310-GINO. Another way to make a telephone number memorable is to group the digits in a memorable way. You could order your pizza from Pizza Hut by calling their ``three tens'' number 3-10-10-10. The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200). The keypad of a phone supplies the mapping of letters to numbers, as follows: A, B, and C map to 2 D, E, and F map to 3 G, H, and I map to 4 J, K, and L map to 5 M, N, and O map to 6 P, R, and S map to 7 T, U, and V map to 8 W, X, and Y map to 9 There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010. Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.) Your company is compiling a directory of telephone numbers from local businesses. As part of the quality control process you want to check that no two (or more) businesses in the directory have the same telephone number. Input The input will consist of one case. The first line of the input specifies the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on the line. The remaining lines list the telephone numbers in the directory, with each number alone on a line. Each telephone number consists of a string composed of decimal digits, uppercase letters (excluding Q and Z) and hyphens. Exactly seven of the characters in the string will be digits or letters. Output Generate a line of output for each telephone number that appears more than once in any form. The line should give the telephone number in standard form, followed by a space, followed by the number of times the telephone number appears in the directory. Arrange the output lines by telephone number in ascending lexicographical order. If there are no duplicates in the input print the line: No duplicates. Sample Input 12 4873279 ITS-EASY 888-4567 3-10-10-10 888-GLOP TUT-GLOP 967-11-11 310-GINO F101010 888-1200 -4-8-7-3-2-7-9- 487-3279 Sample Output 310-1010 2 487-3279 4 888-4567 3
### 回答1: 以下是用 JavaScript 编写匹配邮箱和手机号的代码: ```javascript // 匹配手机号 function isMobile(mobile) { return /^1[3456789]\d{9}$/.test(mobile); } // 匹配邮箱 function isEmail(email) { return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email); } ``` 使用示例: ```javascript console.log(isMobile('13912345678')); // true console.log(isMobile('12345678900')); // false console.log(isEmail('example@example.com')); // true console.log(isEmail('example@com')); // false ``` 以上代码中,`isMobile()` 函数用于匹配手机号,采用正则表达式匹配手机号是否符合格式要求,如果符合则返回 `true`,否则返回 `false`。同理,`isEmail()` 函数用于匹配邮箱,也采用正则表达式匹配邮箱是否符合格式要求。 ### 回答2: 可以使用正则表达式来匹配邮箱和手机号。 对于匹配邮箱的代码,可以使用以下正则表达式: ``` const emailPattern = /^[a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; ``` 这个正则表达式表示邮箱由以下部分组成: - `[a-zA-Z0-9._+-]+`:表示由字母、数字、点号、下划线、加号和减号组成的至少一个字符。 - `@`:表示@符号。 - `[a-zA-Z0-9.-]+`:表示由字母、数字、点号和减号组成的至少一个字符。 - `\.`:表示.号,需要使用转义字符\。 - `[a-zA-Z]{2,}`:表示由字母组成的至少两个字符。 对于匹配手机号的代码,可以使用以下正则表达式: ``` const phonePattern = /^1[3456789]\d{9}$/; ``` 这个正则表达式表示手机号由以下部分组成: - `^1`:表示以1开头。 - `[3456789]`:表示第二位可以是3、4、5、6、7、8或9中的任意一个数字。 - `\d{9}`:表示后面跟着9个数字。 然后,我们可以使用`test`函数来检测一个字符串是否匹配对应的正则表达式。例如: ``` const email = 'example@test.com'; const phone = '13812345678'; if (emailPattern.test(email)) { console.log('邮箱匹配成功'); } else { console.log('邮箱匹配失败'); } if (phonePattern.test(phone)) { console.log('手机号匹配成功'); } else { console.log('手机号匹配失败'); } ``` 以上代码会输出: ``` 邮箱匹配成功 手机号匹配成功 ``` 这样,我们就可以通过正则表达式来匹配邮箱和手机号码了。 ### 回答3: 下面是使用JavaScript编写的匹配邮箱和手机号的代码: ```javascript // 匹配手机号 function matchPhoneNumber(phoneNumber) { var regExp = /^1[3456789]\d{9}$/; return regExp.test(phoneNumber); } // 匹配邮箱 function matchEmail(email) { var regExp = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; return regExp.test(email); } // 测试手机号匹配 console.log(matchPhoneNumber('13812345678')); // 输出 true console.log(matchPhoneNumber('12345678901')); // 输出 false // 测试邮箱匹配 console.log(matchEmail('example@example.com')); // 输出 true console.log(matchEmail('example.com')); // 输出 false ``` 以上代码定义了两个函数`matchPhoneNumber`和`matchEmail`用于匹配手机号和邮箱。它们分别使用了正则表达式来进行匹配,当匹配成功时返回`true`,匹配失败时返回`false`。 在说明这两个函数中,手机号的正则表达式`/^1[3456789]\d{9}$/`用于匹配11位数字,以1开头,第二位数字在3-9之间。而邮箱的正则表达式`/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/`则是一个比较常用的邮箱匹配规则。 最后,我们通过调用`console.log`来测试这两个函数的匹配结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值