473,586 Members | 2,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help on C# Regular Expression

Hi Guys,
I am new to C#. I have a xml string and I want to read a value of a
particular element. I do not want to use dom for this purpose. I think
Regular expression will solve this.

the code snippet goes like this...

string strMsg ="<MyTestXml><F irst>One</First><Second>T wo</Second><Third>T hree</Third></MyTestXml>"

Regex re1=new Regex(@"<First> .*?</First>",RegexOp tions.IgnoreCas e);
string strVal=re1.Matc h(strMsg).Value ;

strVal now contains "<First>One </First>"
How do I read just the value [ in this case, One ] from the
expression. Any help is greatly appreciated.
Nov 15 '05 #1
3 1666
Hello,

Change your pattern to:

@"<First>(.* ?)</First>"

and then inspect the Matches collection. The captured value should be
somewhere in the matches (most likely as a sub-match of a match at index 1).

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Mani Ram" <ma*****@hotmai l.com> wrote in message
news:70******** *************** *@posting.googl e.com...
Hi Guys,
I am new to C#. I have a xml string and I want to read a value of a
particular element. I do not want to use dom for this purpose. I think
Regular expression will solve this.

the code snippet goes like this...

string strMsg ="<MyTestXml><F irst>One</First><Second>T wo</Second><Third>T hree</Third></MyT
estXml>"
Regex re1=new Regex(@"<First> .*?</First>",RegexOp tions.IgnoreCas e);
string strVal=re1.Matc h(strMsg).Value ;

strVal now contains "<First>One </First>"
How do I read just the value [ in this case, One ] from the
expression. Any help is greatly appreciated.


Nov 15 '05 #2


Use this expression:

(?<=<First>).*? (?=</First>)

The (?<=...) and (?=...) constructs are called zero-width positive
look-around assertions because they must be present for a match, but they do
not get included in the match. There are also negative versions (?<!...)
and (?!...).
Brian Davis
www.knowdotnet.com

"Mani Ram" <ma*****@hotmai l.com> wrote in message
news:70******** *************** *@posting.googl e.com...
Hi Guys,
I am new to C#. I have a xml string and I want to read a value of a
particular element. I do not want to use dom for this purpose. I think
Regular expression will solve this.

the code snippet goes like this...

string strMsg ="<MyTestXml><F irst>One</First><Second>T wo</Second><Third>T hree</Third></MyT
estXml>"
Regex re1=new Regex(@"<First> .*?</First>",RegexOp tions.IgnoreCas e);
string strVal=re1.Matc h(strMsg).Value ;

strVal now contains "<First>One </First>"
How do I read just the value [ in this case, One ] from the
expression. Any help is greatly appreciated.

Nov 15 '05 #3
Thank you guys for your reply.

I have worked it this way:

string strMsg
="<mytestxml><f irst>one</first><second>t wo</second><third>t hree</third><
/mytestxml>"

string reMsg = "<first" + "[^>]*" + ">" + "(?<contents>.* ?)" +
"</first>";

Match m1 = Regex.Match(str Msg, reMsg,RegexOpti ons.Multiline |
RegexOptions.Ig noreCase | RegexOptions.Ig norePatternWhit espace);

string s=m1.Groups["contents"].ToString();

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3686
by: Steve | last post by:
Hello, I am writing a script that calls a URL and reads the resulting HTML into a function that strips out everthing and returns ONLY the links, this is so that I can build a link index of various pages. I have been programming in PHP for over 2 years now and have never encountered a problem like the one I am having now. To me this seems...
5
2513
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL, and then reconstruct another URL based on it. For example, I need to scan a web page looking for something like <a...
4
3215
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go over each document, find out if it contains a header and/or a footer and extract only the main content part. The headers and the footers have no...
6
489
by: JohnSouth | last post by:
Hi I've been using a Regular expression to test for valid email addresses. It looks like: \w+(\w+)*@\w+(\w+)*\.\w+(\w+)* I've now had 2 occassions where it has rejected and email address with a "&" character in the local part. I know I should be able to work it out myself, but I'd like to ask anyone to suggest the best way to
3
2283
by: Joe | last post by:
Hi, I have been using a regular expression that I don’t uite understand to filter the valid email address. My regular expression is as follows: <asp:RegularExpressionValidator id="valValidEmail" runat="server" ControlToValidate="txtEmail" ValidationExpression="^(+)(\.+)*@(+)(\.+)*(\.{2,4})$"
1
3700
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am...
3
2554
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular expression. ^(.+?) uses (?!a spoon)\.$
6
2224
by: deepak_kamath_n | last post by:
Hello, I am relatively new to the world of regex and require some help in forming a regular expression to achieve the following: I have an input stream similar to: Slot: slot1 Description: this is a description Slot: slot2
14
2254
by: Chris | last post by:
I need a pattern that matches a string that has the same number of '(' as ')': findall( compile('...'), '42^((2x+2)sin(x)) + (log(2)/log(5))' ) = Can anybody help me out? Thanks for any help!
3
1825
by: Mr.Steskal | last post by:
Posted: Wed Jul 11, 2007 7:01 am Post subject: Regular Expression Help -------------------------------------------------------------------------------- I need help writing a regular expression that only returns part of a string. For Example I have a multi-line text fragment like below:
0
7911
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, 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...
0
8200
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7954
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...
1
5710
isladogs
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...
0
5390
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...
0
3836
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...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
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
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.