Hi,
Supposing I had a string made up of a person's name followed by their
profession in parentheses e.g.
string strText = "Tiger Woods (golfer)";
and I wanted to extract the portion of the string between the parentheses
i.e. "golfer"
Would a regular expression be the most efficient way of doing this...?
I'm trying to do something like this:
string strProfession = String.Empty;
Regex objRegEx = new Regex("(((.|\n) *?))", RegexOptions.Ig noreCase);
foreach (Match objMatch in objRegEx.Matche s(strText)
{
strProfession = objMatch.ToStri ng();
}
but that is returning an empty string, no doubt because I haven't defined
the regular expression correctly.
Also, is it even necessary to have a foreach loop here, as in this
particular scenario there can only ever be one match...?
Any assistance gratefully received.
Mark
Jan 15 '07
16 9374
Mark Rae wrote:
@"\( ([^\)]+) \)", RegexOptions.Ig norePatternWhit espace
is probably a bit simpler and faster - there's no real need to capture
the parens.
That returns an empty string...
No, it doesn't. The Match.Value is the parenthesized expression;
Match.Groups[1].Value is the string in the parens.
--
..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Gary Stephenson wrote:
But perhaps you should be made aware of the limitations implicit in
regexes - the main one being commonly rendered as "regexes can't count". As
long as you are not having to deal with recursive structures, nested
delimiters and so on, regexes will often work well. But they can't be used
to "find the balancing brace", verify correct nesting or suchlike.
Not true with .NET regexes, btw.
--
..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
I haven't been following this whole thread, so I may be somewhat off topic,
but it seems relevant...
Don't forget that you can name groups in a Regex.
For example you could take the expression below and add a name as follows
@"\( (?<StuffIWantTo ActuallySee>[^\)]+) \)",
RegexOptions.Ig norePatternWhit espace
You can then get at it from the Group name.
Match.Groups["StuffIWantToAc tuallySee"].Value
I find that much easier to deal with than trying to get the groups by
number.
Ethan
"Jon Shemitz" <jo*@midnightbe ach.comwrote in message
news:45******** ******@midnight beach.com...
Mark Rae wrote:
@"\( ([^\)]+) \)", RegexOptions.Ig norePatternWhit espace
is probably a bit simpler and faster - there's no real need to capture
the parens.
That returns an empty string...
No, it doesn't. The Match.Value is the parenthesized expression;
Match.Groups[1].Value is the string in the parens.
--
.NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
"Jon Shemitz" <jo*@midnightbe ach.comwrote in message
news:45******** ******@midnight beach.com...
No, it doesn't. The Match.Value is the parenthesized expression;
Match.Groups[1].Value is the string in the parens.
Apologies - my mistake.
Not true with .NET regexes, btw.
Really? How so? They must then be fundamentally different to all regex
implementations I have seen or heard about. A quick scan of the
documentation doesn't reveal anything significantly different about .NET
regexes ... hmmm ...
As I understand it, in order to solve matching-brace type problems, a
push-down automaton is required, as opposed to a finite-state automaton. Do
..NET regexes somehow provide that?
Please explain,
respectfully,
gary
Gary Stephenson wrote:
Not true with .NET regexes, btw.
As I understand it, in order to solve matching-brace type problems, a
push-down automaton is required, as opposed to a finite-state automaton. Do
.NET regexes somehow provide that?
Yes.
..NET capture groups capture all matching expressions, not just the
last one. The "balancing group definition" grouping construct
(?<-name>expr) pops the most recent capture if expr matches; the
(?(name)a|b) alternation construct lets you force the match to fail if
a stack is not empty.
See pgs 289-290 in
<http://www.midnightbea ch.com/.net/ShemitzBook.Cha pter11.pdffor more
details.
--
..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Hi Jon
----- Original Message -----
From: "Jon Shemitz" <jo*@midnightbe ach.com>
Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
Sent: Tuesday, January 16, 2007 12:09 PM
Subject: Re: Using a regular expression to retrieve the text between two
parentheses
.NET capture groups capture all matching expressions, not just the
last one. The "balancing group definition" grouping construct
(?<-name>expr) pops the most recent capture if expr matches; the
(?(name)a|b) alternation construct lets you force the match to fail if
a stack is not empty.
See pgs 289-290 in
<http://www.midnightbea ch.com/.net/ShemitzBook.Cha pter11.pdffor more
details.
Cool as! Thanks for that - very interesting indeed. Apologies to all for
misrepresenting (and underestimating ) .NET regexes.
gary
-- http://www.oxide.net.au This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Daniel |
last post by:
Hello =)
I have an object which contains a method that should execute every x ms. I
can use setInterval inside the object construct like this -
self.setInterval('ObjectName.methodName()', this.pinginterval);
- but is there no way to do this without using the literal ObjectName? If I
write 'this.methodName()' I get "Line 1 Char 1: Object doesn't support this
property or method." in IE, and nothing happens in Firebird.
|
by: Harlin Seritt |
last post by:
I have been looking at the Python re module and have been trying to
make sense of a simple function that I'd like to do. However, no amount
of reading or googling has helped me with this. Forgive my
stone-headedness. I have done this with .NET and Java in the past but
damn if I can't get it done with Python for some reason. As such I am
sure it is something even simpler.
I am trying to find some matches and have them put into a list when...
|
by: MJ |
last post by:
HI
I want to know what is mean by regular expression in C
Mayur
|
by: Lee Kuhn |
last post by:
I am trying the create a regular expression that will essentially match
characters in the middle of a fixed-length string. The string may be any
characters, but will always be the same length. In other words, as the
regular expression (....)($) matches the "4567" in the string "1234567", how
would I create a similar regular expression that only matches the "45" in
the same string. The same regular expression would match "32" in the string...
|
by: Tony Marston |
last post by:
I am seeking help with a regular expression that will split a string into
several parts with ',' (comma) as the separator, but NOT where the separator
is enclosed in parentheses. For example, take the string "field1,
CONCAT(field2,' ', field3) as field23, field4". I would like to be able to
split this into the following:
field1
CONCAT(field2,' ', field3) as field23
field4
Thanks in advance.
| |
by: Pete Davis |
last post by:
I'm using regular expressions to extract some data and some links from some
web pages. I download the page and then I want to get a list of certain
links.
For building regular expressions, I use an app call The Regulator, which
makes it pretty easy to build and test regular expressions.
As a warning, I'm real weak with regular expressions. Let's say my regular
expression is:
|
by: Eric McGraw |
last post by:
In Firefox, as in most regular expression engines, the split function
works like this:
> "His name is <name>. His email address is <email>.".split(/<(.*?)>/)
returns
so that the text pattern in the parentheses is included in the result.
In Internet Explorer, however, the result is different:
|
by: FAQEditor |
last post by:
Anybody have any URL's to tutorials and/or references for Regular
Expressions?
The four I have so far are:
http://docs.sun.com/source/816-6408-10/regexp.htm
http://en.wikipedia.org/wiki/Regular_expression
http://www.regular-expressions.info/javascript.html
http://www.webreference.com/js/column5/
|
by: mw |
last post by:
Hello,
I am trying to make a regex for a 12 hour clock, and I can't seem to
get it just right.
What I have now is
var regexmatch = /(1:|:)\s((a\.m\.)|(p
\.m\.))/i
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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,...
| |
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,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |