Chinaunix

标题: 如何用perl实现:获取定位的一段文字 [打印本页]

作者: scofieldus    时间: 2009-06-23 21:57
标题: 如何用perl实现:获取定位的一段文字
比如说我要获取[Question 1.10] 到 [Question 1.11] 这一段中的文字,请教一下各位如何实现

  1. #!/usr/bin/perl -w
  2. use strict;
  3. my @article=();
  4. #my $sourcefile = $ARGV[0];
  5.         #open(SFHD,"$sourcefile");
  6.         while(<DATA>)
  7.         {
  8.                 if(!@article && /^\s*$/){print; next}
  9.                 push @article,$_;
  10.                 if(/\[Question 1\.10\](.*)\[Question 1\.11\]/s)
  11.                 {
  12.                         print $1;
  13.                 }

  14.         }

  15. __DATA__
  16. [Question 1.10] Which two options will influence the frequency of checkpoints?(Choose two.)
  17. A.the value that is specified for the LOG_CHECKPOINT_TO_ALERT parameter
  18. B.the size of the data files
  19. C.the size of the control file
  20. D.the value that is specified for the PGA_AGGREGATE_TARGET parameter
  21. E.the value that is specified for the FAST_START_MTTR_TARGET parameter
  22. F.the size of redo log files
  23. Correct Answers: E,F
  24. [Question 1.11] Which two statements regarding the database writer (DBWn)
  25. background process are true? (Choose two.)
  26. A.It writes dirty buffers to the data files before the log writer (LGWR)
  27. writes.
  28. B.It is an optional background process.
  29. C.It writes dirty buffers to the data files whenever a transaction
  30. commits.

复制代码

作者: cobrawgl    时间: 2009-06-23 22:27
如果你的格式不变的话,可以这么做

-----------


#!/usr/bin/perl
use strict;
use warnings;

$_ = join '', <DATA>;

print $1 if /\](.*?)\[/s;


__DATA__
[Question 1.10] Which two options will influence the frequency of checkpoints?(Choose two.)
A.the value that is specified for the LOG_CHECKPOINT_TO_ALERT parameter
B.the size of the data files
C.the size of the control file
D.the value that is specified for the PGA_AGGREGATE_TARGET parameter
E.the value that is specified for the FAST_START_MTTR_TARGET parameter
F.the size of redo log files
Correct Answers: E,F
[Question 1.11] Which two statements regarding the database writer (DBWn)
background process are true? (Choose two.)
A.It writes dirty buffers to the data files before the log writer (LGWR)
writes.
B.It is an optional background process.
C.It writes dirty buffers to the data files whenever a transaction
commits.
作者: scofieldus    时间: 2009-06-23 22:45
谢谢啦,
能跟我解释一下
$_ = join '', <DATA>;

这一句吗
作者: cobrawgl    时间: 2009-06-23 22:53
join EXPR,LIST
Joins the separate strings of LIST into a single string with fields separated by the value of EXPR, and returns that new string. Example:

    $rec = join(':', $login,$passwd,$uid,$gid,$gcos,$home,$shell);

Beware that unlike split, join doesn't take a pattern as its first argument. Compare split.


----------

呃,其实就是 perldoc -f join






欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2