473,508 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems with Regular Expression

Hello!

I'm starting to get into RegEx, but am troubled by creating a RegEx.
Basically, I would like to capture any content between (and including) the
first <div> and last </div> tag in a string. I've got the C# code up and
running but could need some help to get me started ..

string regEx = "^?(<div)";

Meaning:
^ (anchor to start of string)
? (zero or one time)
Capture
<div
End Capture

Or am I completely wrong here? I am trying to get into RegEx and am reading
up on the subject. It's a completely new way of thinking your logic, so
please help me out here :-)

<html>
...

(START CAPTURE HERE)
<div>
<div>
</div>
</div>
(END CAPTURE HERE)
</html>

Thanks in advance!

--
venlig hilsen / with regards
anders borum
--
Nov 15 '05 #1
3 1505
Hi,

http://www.desaware.com/Ebook3L2.htm got me going, you should give it a
try. It has some HTML parsing examples.

try this:
(\<div\>.+\</div\>)

'<' and '<' have to be escaped with a '\' inside of capture groups because
they are used to name capture groups.

Mark
"Anders Borum" <na@na.na> wrote in message
news:OF*************@TK2MSFTNGP12.phx.gbl...
Hello!

I'm starting to get into RegEx, but am troubled by creating a RegEx.
Basically, I would like to capture any content between (and including) the
first <div> and last </div> tag in a string. I've got the C# code up and
running but could need some help to get me started ..

string regEx = "^?(<div)";

Meaning:
^ (anchor to start of string)
? (zero or one time)
Capture
<div
End Capture

Or am I completely wrong here? I am trying to get into RegEx and am reading up on the subject. It's a completely new way of thinking your logic, so
please help me out here :-)

<html>
..

(START CAPTURE HERE)
<div>
<div>
</div>
</div>
(END CAPTURE HERE)
</html>

Thanks in advance!

--
venlig hilsen / with regards
anders borum
--

Nov 15 '05 #2
Hi,

Try:

Match m = Regex.Match (textBox1.Text, "^.*?(?'mygroup'<DIV>.*</DIV>).*?$",
RegexOptions.IgnoreCase);

if (m.Success)
Console.Write (m.Groups["mygroup"].Captures[0].ToString());
Some explenation:
^,$ begin and end of sentence, in this case, you need them, because it
must apply to the whole sentence
..*? every character 0 or more, but not greedy, it tries to match as
little as possible
..* every character 0 or more, greedy
() capture group
(?'xxx') named capture group
(?:) group, but do not capture (not used here)
HTH
Greetings
"Anders Borum" <na@na.na> wrote in message
news:OF*************@TK2MSFTNGP12.phx.gbl...
Hello!

I'm starting to get into RegEx, but am troubled by creating a RegEx.
Basically, I would like to capture any content between (and including) the
first <div> and last </div> tag in a string. I've got the C# code up and
running but could need some help to get me started ..

string regEx = "^?(<div)";

Meaning:
^ (anchor to start of string)
? (zero or one time)
Capture
<div
End Capture

Or am I completely wrong here? I am trying to get into RegEx and am reading up on the subject. It's a completely new way of thinking your logic, so
please help me out here :-)

<html>
..

(START CAPTURE HERE)
<div>
<div>
</div>
</div>
(END CAPTURE HERE)
</html>

Thanks in advance!

--
venlig hilsen / with regards
anders borum
--

Nov 15 '05 #3
Just wanted you to know, that I've been working intensively with RegEx in
the past month now, and really see this as an great alternative to many
situations where StringOps would come short.

RegEx are great - if that's not an understatement. Really great - and quite
easy too, once you get the hang of the syntax! Output groups rule! :-)

--
venlig hilsen / with regards
anders borum
--
Nov 15 '05 #4

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

Similar topics

1
4155
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
1
1430
by: Saira | last post by:
Hello al I am not sure where to post this, so I hope this is correct. We have written a Visual Studio AddIn that relies heavily on regular expressions, which are often executed...
4
5100
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
11
5350
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
3
3200
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
7
3794
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
25
5130
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
1
4362
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
1
3380
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
9
1694
by: Tuy Solang | last post by:
I download four scripts that are used to display Khmer PGN for Khmer Chess. It works fine with Netscape 7.1 on MS Millenium, but it gave me an error(Java Script console) with Netscape 8.1 on MS...
0
7132
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...
0
7336
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,...
1
7063
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...
0
5640
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,...
1
5059
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...
0
3211
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...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
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 ...
1
773
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.