473,387 Members | 1,574 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,387 software developers and data experts.

Capturing text between two words with Regex

Hi,
I'm trying to capture text between two words in a multi-line string.
For example:
89. This is the text I would like to capture.
This is another line.
90. End of the example.

I would like to capture text between the words (numbers in this case)
'89' and '90'

Any ideas?
Thanks in advance.

Apr 9 '07 #1
6 16072
did not test in code but try this:
string ResultString = null;
try {
ResultString = Regex.Match(SubjectString, "(?<=89)[a-zA-Z\\.
\\r\\n0-9]*(?=90)").Value;
} catch (ArgumentException ex) {
//error
}
you can replace 89 and 90 with whatever your two words are
if there are other special characters that show up in between you need to
place them in the [] brakets.
hope this get's you started at least...

<ff*****@gmail.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
Hi,
I'm trying to capture text between two words in a multi-line string.
For example:
89. This is the text I would like to capture.
This is another line.
90. End of the example.

I would like to capture text between the words (numbers in this case)
'89' and '90'

Any ideas?
Thanks in advance.

Apr 9 '07 #2
The Regex to do that needs to specify a group to be captured and also allow
multiline captures.

Try this

Regex MyRegex = new
Regex(@"89(?<GroupToCapture>.*)90,RegexOptions.Sin gleLine);
//The parentheses define a group and the ?<name it.
//The option selected allows ".*" to match across multiple lines

MyRegex.Match(Mytext).Groups["GroupToCapture"].Value
I am pretty sure that is correct, but I did it from memory, so my syntax may
be off a little in the line to retrieve the group. I am almost certain the
Regex is correct.

Ethan
<ff*****@gmail.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
Hi,
I'm trying to capture text between two words in a multi-line string.
For example:
89. This is the text I would like to capture.
This is another line.
90. End of the example.

I would like to capture text between the words (numbers in this case)
'89' and '90'

Any ideas?
Thanks in advance.

Apr 9 '07 #3
"Ethan Strauss" <ethan dot strauss at Promega dot comwrote in message
news:uw**************@TK2MSFTNGP06.phx.gbl...
Regex(@"89(?<GroupToCapture>.*)90,RegexOptions.Sin gleLine);
You have to be careful with .*, since it's "greedy". It will grab all the
rest of the characters in the target text, including any "90"s it sees.

You need to use .*? to make the search nongreedy:

89(?<GroupToCapture>.*?)90

///ark
Apr 9 '07 #4
Thanks a lot.
I wanted to capture questions from a text exam. The exam has this
format:
1. First question
a. Answer number one
b. Answer number two
2. Second question
a. ...
b. ...
And so on...
I didn't understand very well the meaning of (?<=...) but now I think
It's like an anchor, isn't it?
I also had problems with the greedy . as Mark Wilden pointed out.

Finally, this is the pattern to capture the questions (?<=\d+)(.+?)(?=
\d+) with the RegexOptions.SingleLine of the text exam.

Thanks.

On 9 abr, 18:28, "deciacco" <a...@a.comwrote:
did not test in code but try this:
string ResultString = null;
try {
ResultString = Regex.Match(SubjectString, "(?<=89)[a-zA-Z\\.
\\r\\n0-9]*(?=90)").Value;} catch (ArgumentException ex) {
//error
}

you can replace 89 and 90 with whatever your two words are
if there are other special characters that show up in between you need to
place them in the [] brakets.
hope this get's you started at least...

<ffre...@gmail.comwrote in message

news:11**********************@d57g2000hsg.googlegr oups.com...
Hi,
I'm trying to capture text between two words in a multi-line string.
For example:
89. This is the text I would like to capture.
This is another line.
90. End of the example.
I would like to capture text between the words (numbers in this case)
'89' and '90'
Any ideas?
Thanks in advance.

Apr 10 '07 #5
There is a problem with the pattern (?<=\d+)(.+?)(?=\d+)
I can't capture question number because group(0) it's only the '(.+?)'
part. The first part of the pattern '(?<=\d+)' it is not capturing.
How can I capture, the question number?
Thanks a lot.
On 10 abr, 13:23, ffre...@gmail.com wrote:
Thanks a lot.
I wanted to capture questions from a text exam. The exam has this
format:
1. First question
a. Answer number one
b. Answer number two
2. Second question
a. ...
b. ...
And so on...
I didn't understand very well the meaning of (?<=...) but now I think
It's like an anchor, isn't it?
I also had problems with the greedy . as Mark Wilden pointed out.

Finally, this is the pattern to capture the questions (?<=\d+)(.+?)(?=
\d+) with the RegexOptions.SingleLine of the text exam.

Thanks.

On 9 abr, 18:28, "deciacco" <a...@a.comwrote:
did not test in code but try this:
string ResultString = null;
try {
ResultString = Regex.Match(SubjectString, "(?<=89)[a-zA-Z\\.
\\r\\n0-9]*(?=90)").Value;} catch (ArgumentException ex) {
//error
}
you can replace 89 and 90 with whatever your two words are
if there are other special characters that show up in between you need to
place them in the [] brakets.
hope this get's you started at least...
<ffre...@gmail.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
Hi,
I'm trying to capture text between two words in a multi-line string.
For example:
89. This is the text I would like to capture.
This is another line.
90. End of the example.
I would like to capture text between the words (numbers in this case)
'89' and '90'
Any ideas?
Thanks in advance.

Apr 10 '07 #6
Ok.
I have tried this pattern (\d+)\.\s+(.+?)\s+(?=\d+\.) with
RegexOptions.Singleline and it works perfectly.
Thanks a lot
On 10 abr, 13:41, ffre...@gmail.com wrote:
There is a problem with the pattern (?<=\d+)(.+?)(?=\d+)
I can't capture question number because group(0) it's only the '(.+?)'
part. The first part of the pattern '(?<=\d+)' it is not capturing.
How can I capture, the question number?
Thanks a lot.

On 10 abr, 13:23, ffre...@gmail.com wrote:
Thanks a lot.
I wanted to capture questions from a text exam. The exam has this
format:
1. First question
a. Answer number one
b. Answer number two
2. Second question
a. ...
b. ...
And so on...
I didn't understand very well the meaning of (?<=...) but now I think
It's like an anchor, isn't it?
I also had problems with the greedy . as Mark Wilden pointed out.
Finally, this is the pattern to capture the questions (?<=\d+)(.+?)(?=
\d+) with the RegexOptions.SingleLine of the text exam.
Thanks.
On 9 abr, 18:28, "deciacco" <a...@a.comwrote:
did not test in code but try this:
string ResultString = null;
try {
ResultString = Regex.Match(SubjectString, "(?<=89)[a-zA-Z\\.
\\r\\n0-9]*(?=90)").Value;} catch (ArgumentException ex) {
//error
}
you can replace 89 and 90 with whatever your two words are
if there are other special characters that show up in between you need to
place them in the [] brakets.
hope this get's you started at least...
<ffre...@gmail.comwrote in message
>news:11**********************@d57g2000hsg.googleg roups.com...
Hi,
I'm trying to capture text between two words in a multi-line string.
For example:
89. This is the text I would like to capture.
This is another line.
90. End of the example.
I would like to capture text between the words (numbers in this case)
'89' and '90'
Any ideas?
Thanks in advance.

Apr 10 '07 #7

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

Similar topics

5
by: TomTom | last post by:
Hi, With C#, is it possible to hover a mouse pointer on the Windows UI controls and show the UI text? For example, can I hover my mouse over "File" in the menu bar and show the text "File" on a...
4
by: Krishna Kumar | last post by:
Hai all, I am doing a project in .net and in that project I have a problem in capturing text from an image. i.e images like CAPTCHA images . which has inbuilt text with in the image.So,...
1
by: mwhite | last post by:
Hi, I need to find a way of capturing the text from a Word document, and storing it. Preferably as a string, as I then need to be able to manipulate the text in code and display it in my web...
0
by: krishnan2408 | last post by:
hi, I want to get all the text that is displayled in a java application window running in the unix OS. I am right now working in the X11::GuiTest module. Can anyone please suggest me any...
1
by: alexvorn2 | last post by:
Hi!, how to get the text that is between < and and it's length is less 10 characters and with out this "/" charachter ? May be you will understand better on this example: Text contains: ...
3
by: mohanprasadgutta | last post by:
Hello, I want to capture the text being printed on console. problem description is as follows. I am calling called.pl program from caller.pl program using backticks. in called.pl program i am...
1
by: Sal Sal | last post by:
I am using System.Text.RegularExpressions.Regex.Replace(input, pattern, replacement, options) in c# to replace the input string. The pattern I am using may have one or more * or . or any other...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.