N
Nurchi BECHED
I have a filename and its process id in brackets.
The problem is, the filename can contain brackets
and numbers in it, but the last number in the
brackets is always the process id.
Now, assume, the process name (3344 is the id):
myfilename.something else ()(321) (3344)
(the actual filename is
"myfilename.something else ()(321) (3344).exe")
How would i get the process id alone, and everything
before it?
like "myfilename.something else ()(321)" and "3344"
What I currently do, is the following:
<code>
string s="myfilename.something else ()(321) (3344).exe";
System.Text.RegularExpressions.MatchCollection mc=
System.Text.RegularExpressions.Regex.Matches(
s, @"\d+");
id=mc[mc.Count-1].Value;
name=System.Text.RegularExpressions.Regex.Match(
s, @".+(\s\(\d+\))??";
</code>
The id part works, even though, I am trying to avoid
using "mc[mc.Count-1]".
Even though, the last number is ALWAYS separated by
a space and it is in brackets, I would still like to
(for learning purposes) do the whole thing at once.
I was trying "\s\(\d+\)" and "\s\(\d+?\)" patterns,
they capture " (3344)" (I mean with a space and both
brackets)
I want to get only the number, but keeping in mind,
that a space followed by a number in brackets could
be a part of the process name as well, it has to be
the last match of the pattern "\s\(\d+\)"
The process name part does not work at all, it just
gets the whole string. I tried ".+\s\("
that returns everything up to (and including) the
space and the opening bracket.
Of course, I could do the first part, and then find
position, and then take a substring from the beginning
to that number, but I was wondering if there is a way
of doing that using regular expressions.
Any help is appreciated.
Thanks in advance.
Have a good day.
--
Regards,
Nurchi BECHED
P.S.
C makes it easy to shoot yourself in the foot;
C++ makes it harder, but when you do,
it blows away your whole leg.
--Bjarne Stroustrup
But C# is the best
--Nurchi BECHED
The problem is, the filename can contain brackets
and numbers in it, but the last number in the
brackets is always the process id.
Now, assume, the process name (3344 is the id):
myfilename.something else ()(321) (3344)
(the actual filename is
"myfilename.something else ()(321) (3344).exe")
How would i get the process id alone, and everything
before it?
like "myfilename.something else ()(321)" and "3344"
What I currently do, is the following:
<code>
string s="myfilename.something else ()(321) (3344).exe";
System.Text.RegularExpressions.MatchCollection mc=
System.Text.RegularExpressions.Regex.Matches(
s, @"\d+");
id=mc[mc.Count-1].Value;
name=System.Text.RegularExpressions.Regex.Match(
s, @".+(\s\(\d+\))??";
</code>
The id part works, even though, I am trying to avoid
using "mc[mc.Count-1]".
Even though, the last number is ALWAYS separated by
a space and it is in brackets, I would still like to
(for learning purposes) do the whole thing at once.
I was trying "\s\(\d+\)" and "\s\(\d+?\)" patterns,
they capture " (3344)" (I mean with a space and both
brackets)
I want to get only the number, but keeping in mind,
that a space followed by a number in brackets could
be a part of the process name as well, it has to be
the last match of the pattern "\s\(\d+\)"
The process name part does not work at all, it just
gets the whole string. I tried ".+\s\("
that returns everything up to (and including) the
space and the opening bracket.
Of course, I could do the first part, and then find
position, and then take a substring from the beginning
to that number, but I was wondering if there is a way
of doing that using regular expressions.
Any help is appreciated.
Thanks in advance.
Have a good day.
--
Regards,
Nurchi BECHED
P.S.
C makes it easy to shoot yourself in the foot;
C++ makes it harder, but when you do,
it blows away your whole leg.
--Bjarne Stroustrup
But C# is the best
--Nurchi BECHED