473,586 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regular Expression:Sear ching for a match within an arbitrary parag

Hello All,
i have been cracking my skull on how to seach a particular reg exp match
within a string? it does not seem to happen except the whole arbitrary string
is the exact match of the regular expression specified....bu t the question is
how do i search for and get all or a number of specified matches within an
arbitrary long string. e.g

for example this is my reg exp i am using:

@"^-[A-Z][a-z]*-$" which will match any string within two full-stops "-"
and"-" example being :
-Cat-

but now how do i search and retreive that match and any other matches from
within a very long string such as:

this is a very long and pointless sentence where i am trying to
retreive-Cat-then -Dog-also something further along this overbearing sentence
at say this point -Man-.

hence retreive -Cat- , -Dog- and -Man-

Can this be done followed by an example please ?

because at the moment i can only match a match if the whole string is a
match! e.g if the whole string i want to find my match from is a match such
as "-Cat-" and that is not what i want i want to get a match or more matches
from a whole load of text...
geez this is giving me a headache just writing this as well!

thanks in advance..
--
The Matrix Insurrection
Nov 16 '05 #1
2 1889
ShadowOfTheBeas t wrote:
for example this is my reg exp i am using:

@"^-[A-Z][a-z]*-$" which will match any string within two full-stops "-"
and"-" example being :

because at the moment i can only match a match if the whole string is a
match! e.g if the whole string i want to find my match from is a match
such as "-Cat-" and that is not what i want i want to get a match or more
matches from a whole load of text...
geez this is giving me a headache just writing this as well!


Your regexp is wrong if you want to do that. Remove the ^ at the beginning
and the $ at the end. They mean "start of text" and "end of text".. so this
regexp only matches if the first and last chars in your text are a "-".

hth,
Max

Nov 16 '05 #2
Thanks Max, i appreciate it i will try it now

"Markus Stoeger" wrote:
ShadowOfTheBeas t wrote:
for example this is my reg exp i am using:

@"^-[A-Z][a-z]*-$" which will match any string within two full-stops "-"
and"-" example being :

because at the moment i can only match a match if the whole string is a
match! e.g if the whole string i want to find my match from is a match
such as "-Cat-" and that is not what i want i want to get a match or more
matches from a whole load of text...
geez this is giving me a headache just writing this as well!


Your regexp is wrong if you want to do that. Remove the ^ at the beginning
and the $ at the end. They mean "start of text" and "end of text".. so this
regexp only matches if the first and last chars in your text are a "-".

hth,
Max

Nov 16 '05 #3

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

Similar topics

4
1643
by: ajikoe | last post by:
Hello, For example I have a string : "Halo by by by" Then I want to take and know the possition of every "by" how can I do it in python? I tried to use: p = re.compile(r"by") m = p.search("Helo by by by")
3
1917
by: Bill Willyerd | last post by:
Is it possible? I have a request to create a stored proc that will dynamically add a range to a WHERE clause based on a numeric value of a comment type. If the incoming comment type request is say 10, the where clause needs to be set to IN(10,11,12,13,14,15,16,17,18,19)OR if a 20 is passed in the clause would read IN(20,21.....) So I was...
3
18744
by: Derek Stone | last post by:
In my continuing inability to completely understand regular expressions I have a new one for you. I'd like to capture a string "A" unless it is anywhere in between string "B" and string "C". Therefore some matches are: XYZAHIJ ABC
4
9169
by: aliensite | last post by:
My code is too greedy, how can it be fixed? Here is my code: Desired output - First:,Second:,Third: <br> <script type="text/javascript"> var regEx = /*?:/g; var html = "<br>First:ratio<br />Second: 2:3<br>Third: size"; var output = html.match(regEx);
4
2148
by: Johnny Lee | last post by:
Hi, I've met a problem in match a regular expression in python. Hope any of you could help me. Here are the details: I have many tags like this: xxx<a href="http://xxx.xxx.xxx" xxx>xxx xxx<a href="wap://xxx.xxx.xxx" xxx>xxx xxx<a href="http://xxx.xxx.xxx" xxx>xxx ..... And I want to find all the "http://xxx.xxx.xxx" out, so I do it
5
2840
by: Paul Johnston | last post by:
Hi Just started using c# to do a task I usually use Perl to do and hit a limit in my knowledge :-) I have a program which reads from a large text file, extracts certain lines then gets a string from these lines. Question If the object I extract is in the form of a number how can I cast it into a number so I can prerform mathematical...
17
11625
by: Randy Webb | last post by:
I know that the /g flag will match all occurrences. Is there a way, with a Regular Expression, to match all occurrences *except* the last one? pattern = /df/g; var myString = "asdfasdfasdfasdf"; var newString = myString.replace(pattern,'gh'); alert(newString) Gives me: asghasghasghasgh as it should. What I want: asghasghasghasdf Where...
2
1927
by: teo | last post by:
match word with interpunctuation Hallo, I need to build a regular expression able to match a specified word, preceded and followed by few chars (mainly interpunctuation) Below the code. ------------------------
6
5439
Markus
by: Markus | last post by:
I'm asking the question for once! Say I have the expression @@ that matches anything other than the characters specified. I also want spaces to be allows, including \n, \t, etc. I've tried adding a simple space into the expression such as @@, but that doesn't seem to work. Anyone know how to do this? I could just match any disallowed...
0
7915
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
7841
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
8204
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. ...
1
7965
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...
0
6617
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...
1
5712
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
5392
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...
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1184
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...

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.