由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Unix版 - regex: what does [^ ] mean?
相关主题
a question about regular expressionregexp help
unix shell help?help on emacs / replace
about vi.how to count string times using perl
a question, thank you![转载] perl: regex
紧急求助!regular expression question
ASK AGAIN!!!Re: How to open/edit a huge text file如何删除从行首到某个特定字符串之间的内容?
what is the cpu usage means in top/gtop?how to delete a file with NULL name in unix? (转载)
XEmacs C++-font-lock custumizing questionRegular expression的问题
相关话题的讨论汇总
话题: two话题: three话题: space话题: matches话题: testswap
进入Unix版参与讨论
1 (共1页)
f***y
发帖数: 98
1
s/^([^ ]+) +([^ ]+)/$2 $1/;
the expression above is to swap the first two words..
what does [^ ] mean? it matches a space at the
first charactor position of a string?
T********r
发帖数: 6210
2
[^ ] means NOT space.

【在 f***y 的大作中提到】
: s/^([^ ]+) +([^ ]+)/$2 $1/;
: the expression above is to swap the first two words..
: what does [^ ] mean? it matches a space at the
: first charactor position of a string?

f***y
发帖数: 98
3
Thank you very much for you answer.
$testswap = "one two three";
$testswap =~ s/^(.+) +(.+)/$2 $1/;
print "$testswap\n";
the screen output is: "three one two"
why? I expected "two one three"..
Thanks.

【在 T********r 的大作中提到】
: [^ ] means NOT space.
c****j
发帖数: 258
4

This is confusing, you use "." to match any character including " "
and excluding "\n", so every parts of your string will be treated as $1
until it meets the last non-space part (from your " +").
say,
s/^(.+) +(.+)/
1. anything from beginning should be assigned to $1 from code "/^(.+)"
2. anything to the end should be assigned to $2 from code "(.+)/"
3. there should be some space between $1 and $2 from code " +"
result: $1 == "one two"
$2 == "three"

【在 f***y 的大作中提到】
: Thank you very much for you answer.
: $testswap = "one two three";
: $testswap =~ s/^(.+) +(.+)/$2 $1/;
: print "$testswap\n";
: the screen output is: "three one two"
: why? I expected "two one three"..
: Thanks.

f***y
发帖数: 98
5
you are right..
Thank you very much.

【在 c****j 的大作中提到】
:
: This is confusing, you use "." to match any character including " "
: and excluding "\n", so every parts of your string will be treated as $1
: until it meets the last non-space part (from your " +").
: say,
: s/^(.+) +(.+)/
: 1. anything from beginning should be assigned to $1 from code "/^(.+)"
: 2. anything to the end should be assigned to $2 from code "(.+)/"
: 3. there should be some space between $1 and $2 from code " +"
: result: $1 == "one two"

p****s
发帖数: 3184
6

Regexp matching is greedy, that is, it will try to match
the longest pattern.
Thus, "^(.+) +" matches "one two ", and $1 is "one two".
The next "(.+)" matches "three", and $2 is "three".

【在 f***y 的大作中提到】
: Thank you very much for you answer.
: $testswap = "one two three";
: $testswap =~ s/^(.+) +(.+)/$2 $1/;
: print "$testswap\n";
: the screen output is: "three one two"
: why? I expected "two one three"..
: Thanks.

f***y
发帖数: 98
7
that's right.. i forgot it's greedy.
Thank you.

【在 p****s 的大作中提到】
:
: Regexp matching is greedy, that is, it will try to match
: the longest pattern.
: Thus, "^(.+) +" matches "one two ", and $1 is "one two".
: The next "(.+)" matches "three", and $2 is "three".

1 (共1页)
进入Unix版参与讨论
相关主题
Regular expression的问题紧急求助!
Am I in trouble?ASK AGAIN!!!Re: How to open/edit a huge text file
Ask help about swap space ( Urgent )what is the cpu usage means in top/gtop?
Re: [转载] HELP:如何产生(0..N-1)共N个"各不相同"的随机数?要求算法最快XEmacs C++-font-lock custumizing question
a question about regular expressionregexp help
unix shell help?help on emacs / replace
about vi.how to count string times using perl
a question, thank you![转载] perl: regex
相关话题的讨论汇总
话题: two话题: three话题: space话题: matches话题: testswap