我正在解析一行从一组parens中提取一个字符串,但在有多个嵌套parens的情况下,当我打印procName时,我没有得到任何东西。
e.g。 109765((测试))blah blah
sscanf(line,"%*d (%[^'('')']", procName);
这种格式应该找到一个left-paren然后返回字符串,同时忽略任意数量的左右parens,但它在这种特殊情况下不起作用。然而,如果只有一套parens它完美无缺。
知道我在这里可能缺少什么吗?在此先感谢!
答案 0 :(得分:2)
我没有回答有关public class WorkoutDetailFragment extends Fragment {
private long workoutId;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_workout_detail, container, false);
}
@Override
public void onStart() {
super.onStart();
View view = getView();
if(view != null){
TextView title = (TextView) view.findViewById(R.id.textTitle);
Workout workout = Workout.workouts[(int)workoutId];
title.setText(workout.getName());
TextView description = (TextView) view.findViewById(R.id.textDescription);
description.setText(workout.getDescription());
}
}
public void setWorkout(long id){
this.workoutId = id;
}
的问题,而是解决了问题,无论有多少括号,都可以解决问题。
sscanf
计划会议:
#include <stdio.h>
#include <string.h>
int main(void) {
char input[1024];
char funcname[33];
char *start, *finis;
size_t len;
if(fgets(input, sizeof input, stdin) == NULL)
return 1;
if ((start = strrchr(input, '(' )) == NULL) // find last '('
return 1;
start++; // move past the bracket
if ((finis = strchr(start, ')' )) == NULL) // find next ')'
return 1;
if ((len = finis - start) >= sizeof funcname)
return 1;
memcpy(funcname, start, len);
funcname [len] = '\0';
printf("%s\n", funcname);
return 0;
}
答案 1 :(得分:2)
OP的"("
正好匹配1 '('
使用"%*[(]"
扫描1个或多个'('
。
最好限制字符串输入。
打破格式输入部分更容易看到
检查返回值。
char procName[100];
int cnt = sscanf(line,"%*d" " %*[(]" "%99[^()']", procName);
if (cnt == 1) OK();