473,385 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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_Name>.*>(?<insideText>.*)</File_Name>");

I am trying to parse this string.

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

Regex pRE = new Regex("<File_Name>.*>(?<insideText>.*)</File_Name>");

I am trying to parse this string.

<File_Name>Services</File_Name>
Regex re = new Regex("<File_Name>(?<insideText>.*)</File_Name>");
string fn = re.Match(s).Groups["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.dkwrote in message
news:47***********************@news.sunsite.dk...
CJ wrote:
>Is this the format to parse a string and return the value between the
item?

Regex pRE = new Regex("<File_Name>.*>(?<insideText>.*)</File_Name>");

I am trying to parse this string.

<File_Name>Services</File_Name>

Regex re = new Regex("<File_Name>(?<insideText>.*)</File_Name>");
string fn = re.Match(s).Groups["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>bbbbbbbbb</file_name><file_name>aaaaaaaaaaaa</file_name>"
you would get this as your whole value:
"bbbbbbbbb</file_name><file_name>aaaaaaaaaaaa". 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_Name>(?<insideText>.*?)</File_Name>");
string fn = re.Match(s).Groups["insideText"].Value;

2) Use a negative Look Ahead
Regex re = new Regex("<File_Name>(?<insideText>((?!</File_Name>).)*)</File_Name>");
string fn = re.Match(s).Groups["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.Singleline);

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_Name>\s*(?<insideText>.*?)\s*</File_Name>");
2)
Regex re = new Regex("<File_Name>\s*(?<insideText>((?!</File_Name>).)*?)\s*</File_Name>");

Kind Regards,

Jesse Houwing
>
CJ

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

Regex pRE = new
Regex("<File_Name>.*>(?<insideText>.*)</File_Name>");

I am trying to parse this string.

<File_Name>Services</File_Name>
Regex re = new Regex("<File_Name>(?<insideText>.*)</File_Name>");
string fn = re.Match(s).Groups["insideText"].Value;

seems to work.

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

"CJ" <cj******@noemail.comwrote in message
news:e6*************@TK2MSFTNGP06.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
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 =...
11
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...
2
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...
4
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...
17
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...
3
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...
22
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...
2
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.