This topic has been archived. It cannot be replied.
-
工作学习 / IT技术讨论 / regular expression brainstormI have some strings like:
[abs3rf].[efg].[23rgd]
[se346eg].[asf34].[345gsg].[g3ge]
….
I want to use regular expression to get the content in last [], such as 23rgd, g3ge according to above examples.
How do you write the regular expression?
Thanks in advance,
-xlego(xlego);
2004-10-28
{273}
(#1952698@0)
-
你讲什么语?
-23456789(大白呼);
2004-10-28
(#1952711@0)
-
/.*\[(.*)\]/
-xanada(㊣流水);
2004-10-28
(#1952715@0)
-
我试了试,我用了2步做到了你的要求,肯定不是最好的,因为我也不是很熟悉1、把最后一个].[和它前面的所有字符干掉
2、把最后一个]干掉
:g/^.*\]\.\[/s///g
:g/\]/s///g
rolia论坛有可能不能把我写的东西正确显示出来
-oceandeep(北极熊·9.11的飞机);
2004-10-28
{144}
(#1952737@0)
-
果然,显示不出来,你只要在每个特殊符号前加上forward slash就可以了
-oceandeep(北极熊·9.11的飞机);
2004-10-28
(#1952741@0)
-
you can use perl to do this.#!C:\Program Files\perl\bin\perl.exe
open(IN, "<file.txt") || die "file.txt can not be opened: $!";
while ($line=<IN>)
{
if ( $line =~ m/.*\[(.*)\]$/) {
print "$1\n";
}
close(IN);
****this will print the result on screen.****
-redspider(忙啥!歇着吧!);
2004-10-28
{258}
(#1952957@0)
-
你忘了给中括号加转义符,嘿嘿
-xanada(㊣流水);
2004-10-28
(#1952969@0)
-
我加了, rolia 不显示。你倒是忘了加$, 楼主是最后中括号内的内容。
-redspider(忙啥!歇着吧!);
2004-10-28
(#1953191@0)
-
哦~~~的确如此,标题显示正确,内容显示不对,看来是个bug。我觉得后面不用加$,说不定 ] 后还有内容呢。。。
-xanada(㊣流水);
2004-10-28
(#1953199@0)
-
\[([^\]]*)\]$
-canadiantire(八卦轮胎);
2004-10-28
(#1953036@0)
-
too easy see my code<PRE>
$a = '[abs3rf].[efg].[23rgd]';
<FONT COLOR="#000055"><B>if</B></FONT> ( $a =~ /\[([a-z0-9]+)\]$/ )
{
<FONT COLOR="#000055"><B>print</B></FONT> $1;
}
-nohairmonk(无毛和尚);
2004-10-31
{175}
(#1957201@0)