-5

I have a string like this:

component.kw1_tap/rt  (path/to/file1/kw1)  (path/to/file2/kw2)

I am able to remove the unwanted chars and produce a clean string like this:

component kw1 tap rt  path to file1 kw1  path to file2 kw2

now i want to remove everything but kw1 and kw2, so my final string should be:

kw1 kw2

Thank you.

EDIT: the strings can change and the position of keywords(kw) can change also.

infinitloop
  • 2,863
  • 7
  • 38
  • 55
  • i don't mind being rated negative, but if it is given with a reason that makes it a little real and something to learn from. – infinitloop Apr 02 '12 at 16:35
  • 1
    Do tell, how is the computer supposed to know what `kw1` and `kw2` are? Are they going to always be the same, is something around them always going to be the same...? That's the kind of info you have to include in your question in order for it to make any sense. – cHao Apr 02 '12 at 16:35
  • sorry if I created confusion here, kw1/kw2 are just keywords i am looking for. The strings can change of of course. – infinitloop Apr 02 '12 at 16:37
  • But what is then a solid part of your string? – w.k Apr 02 '12 at 16:38
  • this is my approach of doing it, if someone has a better one i will be glad to know about it. – infinitloop Apr 02 '12 at 16:39
  • 2
    Perhaps you should give some example Perl code as a setup, showing the string in a scalar variable and the keywords in an array variable. – Ryan C. Thompson Apr 02 '12 at 16:43
  • 1
    You are being downvoted because (a) your question is very vague, and (b) you didn't show any evidence (i.e. code) of having attempted a solution yourself. Please read the [faq] and [ask] for guidelines on how to post on SO. – Jim Garrison Apr 02 '12 at 16:51
  • 1
    I don't think it's vague at all. There's a concrete input string and a very clear output example. It's a very answerable question. – brian d foy Apr 02 '12 at 18:41
  • You didn't specify what distinguishes the strings you want to extract, but I'm guessing it's the last segment of the paths in parens: `my @matches = m{ \( (?:[^)]*/)? ([^/)]+) \) /xg;` – ikegami Apr 02 '12 at 18:49

1 Answers1

2

This is a long shot, as you have given no clue about what is special about kw1 and kw2 that doesn't apply to the rest of the words in your string.

Perhaps this works for you?

use 5.12.0;

my $s = 'component.kw1_tap/rt  (path/to/file1/kw1)  (path/to/file2/kw2)';

my @keywords = $s =~ m|([^/]+?)\s*\)|g;

say for @keywords;

output

kw1
kw2

EDIT

If it is important that the string should be edited in-place, this code uses the same technique

use 5.12.0;

my $s = 'component.kw1_tap/rt  (path/to/file1/kw1)  (path/to/file2/kw2)';

$s = join ' ', $s =~ m|([^/]+?)\s*\)|g;

say $s;

output

kw1 kw2
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • It should be one line output or even better string modification... – Ωmega Apr 02 '12 at 16:48
  • @downvoter: please let me know the problem with my answer? – Borodin Apr 02 '12 at 16:49
  • Just wondering...what about this code requires perl v5.12? – cHao Apr 02 '12 at 16:52
  • No reason to use `\s*` and no reason to use temp variable... – Ωmega Apr 02 '12 at 16:53
  • @stackoverflow: It's a tiny change to show the results on one line: just `say "@keywords";` will do the trick. I have edited my answer to show how to edit the string in-place. Were you the downvoter? A bit harsh don't you think? – Borodin Apr 02 '12 at 16:55
  • @cHao: I use it to bundle `strict`, `warnings`, and `feature 'say'` into one `use`, at the same time documenting which platform I used to test. – Borodin Apr 02 '12 at 16:56
  • @Borodin - Thank you for being my **teacher** :) No -1 from me, no +1 from me... – Ωmega Apr 02 '12 at 16:57
  • @stackoverflow: *please* be constructive. The `\s*` is there in case the real-world examples had whitespace there. It doesn't break the code. Temporary variables can often make the operation of the program clearer, and it was my intention here that the `my @keywords = ` line is the solution I am offering and the `say` is there just to document the correctness. – Borodin Apr 02 '12 at 16:58
  • @Borodin - believe or not, I know what `\s*` is there for, but author didn't ask for that... Anyway - this question is a mess... so it was closed... – Ωmega Apr 02 '12 at 16:59