문자 파싱하는 tip

은빛연어의 이미지

^^*
문자를 파싱하기 위해서 머리 굴려가면서 for문 돌려가면서 플레그 온오프시키야져.. 후아~~ 복잡하다.. ㅋㅋ
물론 복잡한 파싱을 할경우엔 파싱 함수를 만들어야겟찌만 얼마 길지 않는 경우에는 쉽게 처리할 수 있습니다.. ^^* 참고하세요~~

main()
{
	char *temp0="abc : 123";
	char *temp1="abc:123";
	char *temp2="abc :123";
	char *temp3="abc: 123";
	char prebuf[16], value[16];

	/*value의 결과가 동일합니다.*/
	sscanf(temp0, "%[^:]:%s", prebuf, value);
	printf("[%s][%d]\n", prebuf, atoi(value));
	
	sscanf(temp1, "%[^:]:%s", prebuf, value);
	printf("[%s][%d]\n", prebuf, atoi(value));

	sscanf(temp2, "%[^:]:%s", prebuf, value);
	printf("[%s][%d]\n", prebuf, atoi(value));

	sscanf(temp3, "%[^:]:%s", prebuf, value);
	printf("[%s][%d]\n", prebuf, atoi(value));

}

sscanf에는 이외에도 여러가지 기능들이 있습니다. 조사해 보세요~~ 재미있을꺼예요 ^^*

Forums: 
cedar의 이미지

참고로 scanf의 Format Specifier로 정규식을 쓰는
sscanf(temp0, "%[^:]:%s", prebuf, value);
와 같은 문법은 ANSI C에는 없습니다.
다른 플랫폼에서는 사용할 수 없습니다. 대신 strtok를 써야겠지요.

jj의 이미지

cedar wrote:
참고로 scanf의 Format Specifier로 정규식을 쓰는
sscanf(temp0, "%[^:]:%s", prebuf, value);
와 같은 문법은 ANSI C에는 없습니다.
다른 플랫폼에서는 사용할 수 없습니다. 대신 strtok를 써야겠지요.

어떤게 정규식 인지요?

--
Life is short. damn short...

cedar의 이미지

jj wrote:
어떤게 정규식 인지요?

%[^:]부분입니다.
eungkyu의 이미지

cedar wrote:
jj wrote:
어떤게 정규식 인지요?

%[^:]부분입니다.

[^:], 이 부분이 정규식이라면, 한글자만 매칭이 되어야 할텐데 :(

정규식, regular expression이라는 단어를 함부로 쓰면 안되겠죠.
그냥 일정 패턴에 따라 받을 수 있다고 쓰는게 좋을 거 같습니다.

지리즈의 이미지

eungkyu wrote:
cedar wrote:
jj wrote:
어떤게 정규식 인지요?

%[^:]부분입니다.

[^:], 이 부분이 정규식이라면, 한글자만 매칭이 되어야 할텐데 :(

정규식, regular expression이라는 단어를 함부로 쓰면 안되겠죠.
그냥 일정 패턴에 따라 받을 수 있다고 쓰는게 좋을 거 같습니다.

"%[^:]:%s" 이거 이상하지 않나요?

There is no spoon. Neo from the Matrix 1999.

은빛연어의 이미지

음... 궁금해 하시는 분들이 많이 있네요.. ^^
cc나 gcc man-page를 보시면 답이 있습니다... 궁금해하시는분들 man-page에 주저리주저리 나와있는 영어를 먼저 분석하시길~~~ ^^*

참고로 컴터는 바봅니다.. 컴파일러에서 컴터가 아는것으로 바꿔주기전까진.. ^^+
(질문있으신분들~~ 부디 맨페이지부터 정독해주세요~~ ^^)

Quote:

>>>> man sscanf의 일부분
[ Matches a non-empty sequence of characters from a set
of expected characters (the scanset). The normal skip
over white-space characters is suppressed in this
case. The corresponding argument must be a pointer to
the initial byte of an array of char, signed char, or
unsigned char large enough to accept the sequence and
a terminating null byte, which will be added automati-
cally.

If an l (ell) qualifier is present, the input is a
sequence of characters that begins in the initial
shift state. Each character in the sequence is con-
verted to a wide-character as if by a call to the
mbrtowc() function, with the conversion state
described by an mbstate_t object initialized to zero
before the first character is converted. The
corresponding argument must be a pointer to an array
of wchar_t large enough to accept the sequence and the
terminating null wide-character, which will be added
automatically.

The conversion specification includes all subsequent
characters in the format string up to and including
the matching right square bracket (]). The characters
between the square brackets (the scanlist) comprise
the scanset, unless the character after the left
square bracket is a circumflex (^), in which case the
scanset contains all characters that do not appear in
the scanlist between the circumflex and the right
square bracket. If the conversion specification begins
with [] or [^], the right square bracket is included
in the scanlist and the next right square bracket is
the matching right square bracket that ends the
conversion specification; otherwise the first right
square bracket is the one that ends the conversion
specification. If a - is in the scanlist and is not
the first character, nor the second where the first
character is a ^, nor the last character, it indicates
a range of characters to be matched.

[/code]
익명 사용자의 이미지

eungkyu I agree with your words.

jbkdd의 이미지

관련 없을 지도 모르지만
msdn printf페이지의 문자열 포멧 관련 링크에서도 자세히 나와있군요.
msdn 가지고 계신 분들은 확인해 보시길..

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.