Question for RegEx gurus

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
 
N

Nurchi BECHED

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")


(.+)\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
 
F

Frank Schmitt

Nurchi said:
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
 
N

Nurchi BECHED

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.


Nurchi said:
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
 
F

Frank Schmitt

Nurchi said:
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
 
N

Nurchi BECHED

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)


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top