473,473 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Question for RegEx gurus

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
6 1436
Doesn't (.+)\s(\(\d+)\) work?

Regards
Senthil

Nov 16 '05 #2
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Miki Tebeka | last post by:
Hello All, To all of you regexp gurus out there... I'd like to find all of the sub strings in the form "add(.*)" The catch is that I might have () in the string (e.g. "add((2 * 2), 100)"), ...
5
by: Ali Eghtebas | last post by:
Hi, I've made this regex to catch the start of a valid multiline comment such as "/*" in e.g. T-SQL code. "(?<=^(?:*'*')*?*)(?<!^(?:*'*')*?--.*)/\*.*?$" With Multiline option on. As we know...
7
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b)...
4
by: Ali Eghtebas | last post by:
Hi, I've made this regex to catch the start of a valid multiline comment such as "/*" in e.g. T-SQL code. "(?<=^(?:*'*')*?*)(?<!^(?:*'*')*?--.*)/\*.*?$" With Multiline option on. As we know...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.