473,788 Members | 2,548 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16111
did not test in code but try this:
string ResultString = null;
try {
ResultString = Regex.Match(Sub jectString, "(?<=89)[a-zA-Z\\.
\\r\\n0-9]*(?=90)").Value ;
} catch (ArgumentExcept ion 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.goo glegroups.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(?<Gr oupToCapture>.* )90,RegexOption s.SingleLine);
//The parentheses define a group and the ?<name it.
//The option selected allows ".*" to match across multiple lines

MyRegex.Match(M ytext).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.goo glegroups.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******** ******@TK2MSFTN GP06.phx.gbl...
Regex(@"89(?<Gr oupToCapture>.* )90,RegexOption s.SingleLine);
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(?<GroupToCap ture>.*?)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.Si ngleLine of the text exam.

Thanks.

On 9 abr, 18:28, "deciacco" <a...@a.comwrot e:
did not test in code but try this:
string ResultString = null;
try {
ResultString = Regex.Match(Sub jectString, "(?<=89)[a-zA-Z\\.
\\r\\n0-9]*(?=90)").Value ;} catch (ArgumentExcept ion 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.goo glegroups.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.c om 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.Si ngleLine of the text exam.

Thanks.

On 9 abr, 18:28, "deciacco" <a...@a.comwrot e:
did not test in code but try this:
string ResultString = null;
try {
ResultString = Regex.Match(Sub jectString, "(?<=89)[a-zA-Z\\.
\\r\\n0-9]*(?=90)").Value ;} catch (ArgumentExcept ion 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.goo glegroups.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.Si ngleline and it works perfectly.
Thanks a lot
On 10 abr, 13:41, ffre...@gmail.c om 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.c om 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.Si ngleLine of the text exam.
Thanks.
On 9 abr, 18:28, "deciacco" <a...@a.comwrot e:
did not test in code but try this:
string ResultString = null;
try {
ResultString = Regex.Match(Sub jectString, "(?<=89)[a-zA-Z\\.
\\r\\n0-9]*(?=90)").Value ;} catch (ArgumentExcept ion 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.go oglegroups.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
9612
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 label? I've seen a program like this but not sure if it can be done with C#. Thanks! TomTom
4
2793
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, Can any one suggest me how to continue my work or give their valuable suggestions for capturing text in the CAPTCHA image . yours KrishnaKumar
1
1334
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 app. Please help! Thanks in advance Mat
0
1143
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 module(or function) to fulfill this requirement? If this is not possible, is it possible to capture the window into a .jpg file(say) and then get the text from it. Please suggest some modules which will work in the unix OS. I don't think the...
1
1103
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: //This text to get because the text length is more that 10 characters < The quick brown fox jumps over the lazy dog
3
2221
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 giving an external command to be executed in backticks. that external application will through a message to the console if any arguements are missing for that command. for example if we are not sending username and password(or wrong username and...
1
3519
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 character basically inside mixed in with the rest of the string. I want to find this string in the input and replace it with something. For example say my string is *000000000 I get the following error System.ArgumentException: parsing...
0
9499
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10177
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10121
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8995
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7519
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5539
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2898
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.