我正在尝试从某些文本中提取电话号码
问题是我得到了4个不同的匹配项,在这里我只想要此特定表达式的完整匹配项。 例如,我得到:
public async static Task Main(string[] args)
{
TcpClient tc = new TcpClient();
await tc.ConnectAsync("smtp-mail.outlook.com", 587);
MyStream ms = new MyStream(tc);
byte[] response = new byte[64*1024];
int n;
string command;
byte[] request;
n = await ms.ReadAsync(response, 0, response.Length);
command = "EHLO domain\r\n";
request = Encoding.ASCII.GetBytes(command);
await ms.WriteAsync(request, 0, request.Length);
n = await ms.ReadAsync(response, 0, response.Length);
Console.WriteLine(Encoding.ASCII.GetString(response, 0, n));
command = "STARTTLS\r\n";
request = Encoding.ASCII.GetBytes(command);
await ms.WriteAsync(request, 0, request.Length);
n = await ms.ReadAsync(response, 0, response.Length);
Console.WriteLine(Encoding.ASCII.GetString(response, 0, n));
SslStream ssl = new SslStream(ms);
await ssl.AuthenticateAsClientAsync("smtp-mail.outlook.com");
command = "RSET\r\n";
request = Encoding.ASCII.GetBytes(command);
await ssl.WriteAsync(request, 0, request.Length);
n = await ssl.ReadAsync(response, 0, response.Length);
Console.WriteLine(Encoding.ASCII.GetString(response, 0, n));
command = "QUIT\r\n";
request = Encoding.ASCII.GetBytes(command);
await ssl.WriteAsync(request, 0, request.Length);
n = await ssl.ReadAsync(response, 0, response.Length);
Console.WriteLine(Encoding.ASCII.GetString(response, 0, n));
ssl.Dispose();
tc.Close();
}
如您所见,我只需要该列表中的第一个匹配项。
这是我的代码:
Match 1
1. 054-434-4321
2. 054
3. -
4. -
Match 2
1. (03) 502 9571
2. (03)
3.
4.
这是正则表达式:
text = "You can reach me at 054-434-4321, or my office at (03) 502 9571 or (050) 223 957.\
Send me a fax at 03 502 7422. We finally made the sale for all 977 giraffes.\
They wanted 225 957 dollars for it"
phone_pattern = re.compile(r'(\d{2,3}|\(\d{2,3}\))(-| )\d{3}(-| )\d{3,4})')
phone_results = phone_pattern.findall(text)
print(f'extracted {len(phone_results)} results : {phone_results}')
为了不对结果进行分组,我试图将括号放在表达式的末尾。
答案 0 :(得分:1)
import re
text = """
You can reach me at 054-434-4321, or my office at (03) 502 9571 or (050) 223 957.
Send me a fax at 03 502 7422. We finally made the sale for all 977 giraffes.
They wanted 225 957 dollars for it.
"""
phone_pattern = re.compile(r'(?:\d{2,3}|\(\d{2,3}\))(?:-| )\d{3}(?:-| )\d{3,4}')
for result in phone_pattern.findall(text):
print(result)
输出
054-434-4321
(03) 502 9571
(050) 223 957
03 502 7422
答案 1 :(得分:0)
简单地为:
Change
输出:
fetch('https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1')
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(myJson);
});