473,770 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
+ 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.some thing else ()(321) (3344)

(the actual filename is
"myfilename.som ething else ()(321) (3344).exe")

How would i get the process id alone, and everything
before it?
like "myfilename.som ething else ()(321)" and "3344"

What I currently do, is the following:
<code>
string s="myfilename.s omething else ()(321) (3344).exe";

System.Text.Reg ularExpressions .MatchCollectio n mc=
System.Text.Reg ularExpressions .Regex.Matches(
s, @"\d+");
id=mc[mc.Count-1].Value;
name=System.Tex t.RegularExpres sions.Regex.Mat ch(
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 1454
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.some thing else ()(321) (3344)

I want:
myfilename.some thing else ()(321)

and:
3344

Thanks.

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

(I had: "myfilename.som ething else ()(321) (3344).exe")
On 22 Feb 2005 22:04:28 -0800, sadhu <se**********@w devs.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.some thing else ()(321) (3344)

I want:
myfilename.some thing 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*******@SPAM quattro-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.some thing else ()(321) (3344)
I want:
myfilename.some thing 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.s omething else () (321) (1184)
^^ ^ (Note space, and brackets
here)

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

string z="name: " + m1.Groups["name"] + "\r\nid: " + m1.Groups["id"];
'z' will contain:
name: ProcessViewer.s omething 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*******@SPAM quattro-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
1397
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)"), Currently I can only get the "addr((2 *2)" using re.compile("\w+\(*\)"). To solve the problem a hand crafted search is used :-(
5
2144
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 the T-SQL single line comment starts with a "--" and the string character is a "'". Considering all this, from these lines below the pattern will only catch "/*
7
2618
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) every occurrence of characters "p", "q", "r", "s" with "y". Right now, I do it as follows:
4
252
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 the T-SQL single line comment starts with a "--" and the string character is a "'". Considering all this, from these lines below the pattern will only catch "/*
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10101
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8933
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.