473,573 Members | 5,087 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parsing in between strings using Regex

CJ
Is this the format to parse a string and return the value between the item?

Regex pRE = new Regex("<File_Na me>.*>(?<inside Text>.*)</File_Name>");

I am trying to parse this string.

<File_Name>Serv ices</File_Name>
Thanks
Mar 3 '08 #1
4 2665
CJ wrote:
Is this the format to parse a string and return the value between the item?

Regex pRE = new Regex("<File_Na me>.*>(?<inside Text>.*)</File_Name>");

I am trying to parse this string.

<File_Name>Serv ices</File_Name>
Regex re = new Regex("<File_Na me>(?<insideTex t>.*)</File_Name>");
string fn = re.Match(s).Gro ups["insideText "].Value;

seems to work.

Arne
Mar 3 '08 #2
CJ
Thanks Arne,

Seems like the ".*" was messing me up.

This regular expression is so hard at times, I don't know how
you guys have this thing figured out.

CJ
"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:47******** *************** @news.sunsite.d k...
CJ wrote:
>Is this the format to parse a string and return the value between the
item?

Regex pRE = new Regex("<File_Na me>.*>(?<inside Text>.*)</File_Name>");

I am trying to parse this string.

<File_Name>Ser vices</File_Name>

Regex re = new Regex("<File_Na me>(?<insideTex t>.*)</File_Name>");
string fn = re.Match(s).Gro ups["insideText "].Value;

seems to work.

Arne

Mar 3 '08 #3
Hello cj,
Thanks Arne,

Seems like the ".*" was messing me up.

This regular expression is so hard at times, I don't know how you guys
have this thing figured out.
This looks a lot like XML data. If it is, you really should try to avoid
regex and use XPath to fetch the data you need.

If it isn't wellformed Regex can help you, but the regex you have still has
a few issues in it.

dor one, if your input would contain "<file_name>bbb bbbbbb</file_name><file _name>aaaaaaaaa aaa</file_name>"
you would get this as your whole value:
"bbbbbbbbb</file_name><file _name>aaaaaaaaa aaa". Obviously not what's required.

You can adjust your regex to prevent this from happening in two ways:

1) Use Reluctant Matching
Regex re = new Regex("<File_Na me>(?<insideTex t>.*?)</File_Name>");
string fn = re.Match(s).Gro ups["insideText "].Value;

2) Use a negative Look Ahead
Regex re = new Regex("<File_Na me>(?<insideTex t>((?!</File_Name>).)*) </File_Name>");
string fn = re.Match(s).Gro ups["insideText "].Value;

One thing that migth also catch up with you is afile that is formatted like
this (let's hope the newsreader will leave this in tact):
<file_name>
bbbbbbbbb
</file_name>

This is probably syntactically correct, but as . normally doesn't match over
the end of a line, it will require you to use an extra switch in your regex
constructor (either case) which will allow . to match newline.
Regex re = new Regex("your regex here", RegexOptions.Si ngleline);

Alternatively you could 'eat up' all whitespace around the File_Name. But
only if you're very sure the filename itself will never contain a newline
or have whitespace in it at the strat or end of the filename.

1)
Regex re = new Regex("<File_Na me>\s*(?<inside Text>.*?)\s*</File_Name>");
2)
Regex re = new Regex("<File_Na me>\s*(?<inside Text>((?!</File_Name>).)*? )\s*</File_Name>");

Kind Regards,

Jesse Houwing
>
CJ

"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:47******** *************** @news.sunsite.d k...
>CJ wrote:
>>Is this the format to parse a string and return the value between
the item?

Regex pRE = new
Regex("<File_ Name>.*>(?<insi deText>.*)</File_Name>");

I am trying to parse this string.

<File_Name>Se rvices</File_Name>
Regex re = new Regex("<File_Na me>(?<insideTex t>.*)</File_Name>");
string fn = re.Match(s).Gro ups["insideText "].Value;

seems to work.

Arne
--
Jesse Houwing
jesse.houwing at sogeti.nl
Mar 3 '08 #4
Hi,

"CJ" <cj******@noema il.comwrote in message
news:e6******** *****@TK2MSFTNG P06.phx.gbl...
Thanks Arne,

Seems like the ".*" was messing me up.

This regular expression is so hard at times, I don't know how
you guys have this thing figured out.
Practice, you should try it a couple of times until you find the correct way

Also a book would help you ;)

Mar 3 '08 #5

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

Similar topics

7
332
by: Daniel Lidström | last post by:
Hi, I'm currently using this method to extract doubles from a string: System::String* sp = S" "; System::String* tokens = s->Trim()->Split(sp->ToCharArray()); m_Northing = System::Double::Parse(tokens, nfi); m_Easting = System::Double::Parse(tokens, nfi); m_Elevation = System::Double::Parse(tokens, nfi);
11
3888
by: Martin Robins | last post by:
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being parsed can completely wreck it. The string I am trying to parse is as follows: commandText=insert into (Text) values (@message + N': ' +...
2
2181
by: Julie | last post by:
I have to process parameter input strings of key/value pairs /key=value where value _may_ contain a slash /. Here is a sample input string: /def=gamma chain (FT2) /name=NULL /p_id=CF11423:SF7 /fn=Defense/receptor member /process=Signal transduction /len=98 Here is the regex:
4
3168
by: Jozef Jarosciak | last post by:
Hi everyone, I am building a web crawler and one of the features which I need to include is exclusion of specified 'variable + value' from the url. Example, user wanted to extract variable "s": So when you look at this url: "http://www.goldenretrieverforum.com/search.php?s=5817617a59fb630a7f40846e4a29efc1&do=getdaily"
17
2774
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions might be a neat way to solve this, but I am new to them. Can anyone give me a hint here? The catch is, it must only find tokens that are not quoted...
3
2536
by: Chris | last post by:
Hi everyone, I'm trying to parse through the contents of some text files with regular expressions, but am new to regular expressions and how to use them in VB.net. I'm pretty sure that the regular expressions are correct as I got them from regexlib.com and tested them in the Regulator and Expresso. The problem is I tested this function...
22
1500
by: gene.ariani | last post by:
I have a string like the following: 10AF101-25 I would like to extract any numerical number that precedes the "-" and stops when it encounters any string character like AF So my result should be 101.
2
1196
by: Advait Mohan Raut | last post by:
Hello friends, I want to parse a string which has many tokens. Some tokens may be present or absent. eg. A --B,C,D,E ; B --b,M,N | null ; C --O,c, (P|Q) ;
6
1603
by: John Rogers | last post by:
Can someone show me how to parse a string to find a specific value? <b><a id="wt2500_WC_xc2500_GVB_drtl00_WQR_xt400_G" href="/WW/XZ/LinkToDetailsList.asp">Details List Filers</a></b> That is my string, I have thousands of lines to go through, I am looking to get back the following value: "Details List Filers" These are unique in the...
0
7792
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
7709
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...
0
8039
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. ...
0
8091
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 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...
1
5605
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
5296
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
3738
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...
1
2228
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
1330
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.