Home > Archive > PERL Beginners > March 2005 > regexp help
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Abhishek Dave 2005-03-28, 8:56 pm |
| Hello ,
i am sticking with a problem to replace all words of a file in such a =
manner that the first and second word need to interchanged.
some thing like ...
100000 100001 20000 20001 ....
so that the output file will be
100001 10000 20001 20000 ....
any helpful regex ???
thanks
| |
| Hendrik Maryns 2005-03-28, 8:56 pm |
| Abhishek Dave schreef:
> Hello ,
>
> i am sticking with a problem to replace all words of a file in such a =
> manner that the first and second word need to interchanged.
> some thing like ...
>
> 100000 100001 20000 20001 ....
>
>
>
> so that the output file will be
>
> 100001 10000 20001 20000 ....
>
>
>
> any helpful regex ???
Just a try, untested:
s/^\s*(.*?)\s(.*?)/$2 $1/
Not sure the ? in *? are necessary though.
HTH, H.
| |
| Jeff 'japhy' Pinyan 2005-03-28, 8:56 pm |
| On Mar 28, Abhishek Dave said:
> i am sticking with a problem to replace all words of a file in such a =
> manner that the first and second word need to interchanged.
> some thing like ...
>
> 100000 100001 20000 20001 ....
>
> so that the output file will be
>
> 100001 10000 20001 20000 ....
The easiest way I can think of is:
s/(\S+)(\s+)(\S+)/$3$2$1/g;
That reverses every pair of fields, and retains the whitespace that was
between them.
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
|
|
|
|
|