By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,808 Members | 1,612 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,808 IT Pros & Developers. It's quick & easy.

Question for RegEx gurus

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Doesn't (.+)\s(\(\d+)\) work?

Regards
Senthil

Nov 16 '05 #2

P: n/a
No, that "(.+)\s(\(\d+)\)" does the same as ".+"
It simply returns the whole process name with the process id.
I need the whole thing, but without the last brackets (and the number
inside them)
So, basically, I have this:
myfilename.something else ()(321) (3344)

I want:
myfilename.something else ()(321)

and:
3344

Thanks.

SORRY, IN MY LAST POST, THE ACTUAL FILENAME IS:
myfilename.something else ()(321).exe

(I had: "myfilename.something else ()(321) (3344).exe")
On 22 Feb 2005 22:04:28 -0800, sadhu <se**********@wdevs.com> wrote:
(.+)\s(\(\d+)\)


--
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
Nov 16 '05 #3

P: n/a
Nurchi BECHED wrote:
No, that "(.+)\s(\(\d+)\)" does the same as ".+"
It simply returns the whole process name with the process id.
I need the whole thing, but without the last brackets (and the number
inside them)
So, basically, I have this:
myfilename.something else ()(321) (3344)

I want:
myfilename.something else ()(321)

and:
3344


The following should work:

// use named groups for the name and the id part
// id is of the form (123), i.e. a number in brackets => \(\d+\)
// name is any character sequence => .*
Match m = Regex.Match(s, @"(?<name>.*)(?<id>\(\d+\))");
System.Console.WriteLine("name: " + + ",id: " + m.Groups["id"]);

HTH & kind regards
frank
Nov 16 '05 #4

P: n/a
Thanks Frank, that really helped,
but I still had that thing in my mind about
getting the id without the brackets.
Again, I can always take a substring of the result from
index 1 to (Length-2) and get what I need, but I wanted
to know (rather just curious) if RegEx can do that for me.

Thanks again, your last post really helped me, and saved
10-20 lines of code going through a string in a loop, and
playing with individual characters...

Good luck.
On Wed, 23 Feb 2005 09:28:18 +0100, Frank Schmitt
<sc*******@SPAMquattro-researchPLEASE.com> wrote:
Nurchi BECHED wrote:
No, that "(.+)\s(\(\d+)\)" does the same as ".+"
It simply returns the whole process name with the process id.
I need the whole thing, but without the last brackets (and the number
inside them)
So, basically, I have this:
myfilename.something else ()(321) (3344)
I want:
myfilename.something else ()(321)
and:
3344


The following should work:

// use named groups for the name and the id part
// id is of the form (123), i.e. a number in brackets => \(\d+\)
// name is any character sequence => .*
Match m = Regex.Match(s, @"(?<name>.*)(?<id>\(\d+\))");
System.Console.WriteLine("name: " + + ",id: " + m.Groups["id"]);

HTH & kind regards
frank


--
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
Nov 16 '05 #5

P: n/a
Nurchi BECHED wrote:
Thanks Frank, that really helped,
but I still had that thing in my mind about
getting the id without the brackets.


Then use

Match m = Regex.Match(s, @"(?<name>.*)\((?<id>\d+)\)");

Here, the parentheses "\(" and "\)" are defined outside of the id -
the id contains only the numbers.

HTH & kind regards
frank
Nov 16 '05 #6

P: n/a
Thanks, Frank
I appreciate your help.
Isn't it amazing that you get new ideas after submitting a post...

For those, who are interested, all pieces together:
string s=
ProcessViewer.something else () (321) (1184)
^^ ^ (Note space, and brackets
here)

System.Text.RegularExpressions.Match m1=
System.Text.RegularExpressions.Regex.Match(s,
@"(?<name>.*)\s\((?<id>\d+)\)");

string z="name: " + m1.Groups["name"] + "\r\nid: " + m1.Groups["id"];
'z' will contain:
name: ProcessViewer.something else () (321)
id: 1184
Thanks again, Frank.
I really appreciate your help
I hope, there will be something, I can help you with

(BTW, I am Avar (Hun), if it means anything to you)
On Fri, 25 Feb 2005 09:21:51 +0100, Frank Schmitt
<sc*******@SPAMquattro-researchPLEASE.com> wrote:
Nurchi BECHED wrote:
Thanks Frank, that really helped,
but I still had that thing in my mind about
getting the id without the brackets.


Then use

Match m = Regex.Match(s, @"(?<name>.*)\((?<id>\d+)\)");

Here, the parentheses "\(" and "\)" are defined outside of the id -
the id contains only the numbers.

HTH & kind regards
frank


--
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
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.