我用getline函数来读取一行STDIN.
原型getline是:
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
Run Code Online (Sandbox Code Playgroud)
我使用它作为测试程序,来自http://www.crasseux.com/books/ctutorial/getline.html#getline
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int atgc, char *argv[])
{
int bytes_read = 1;
int nbytes = 10;
char *my_string;
my_string = (char *)malloc(nbytes+1);
puts("Please enter a line of text");
bytes_read = getline(&my_string, &nbytes, stdin);
if (bytes_read == -1)
{
puts ("ERROR!");
}
else
{
puts ("You typed:");
puts (my_string);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这很好用.
我的怀疑是?
为什么使用char **lineptr而不是char …
我正试图将这些[0-9]和[A-Z]字符串分开:
100M
20M1D80M
20M1I79M
20M10000N80M
Run Code Online (Sandbox Code Playgroud)
我尝试使用Python re模块,以下是我使用的代码:
>>>import re
>>>num_alpha = re.compile('(([0-9]+)([A-Z]))+')
>>>str1="100M"
>>>n_a_match = num_alpha.match(str1)
>>>n_a_match.group(2), n_a_match.group(3)
100,M #just what I want
>>>str1="20M10000N80M"
>>>n_a_match = num_alpha.match(str1)
>>>n_a_match.groups()
('80M', '80', 'M') #only the last one, how can I get the first two?
#expected result ('20M','20','M','10000N','10000','N','80M','80','M')
Run Code Online (Sandbox Code Playgroud)
此正则表达式适用于仅包含一个匹配但不包含多个匹配组的字符串.如何使用正则表达式处理它?