Regular Expression:Searching for a match within an arbitrary parag

G

Guest

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....but 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..
 
M

Markus Stoeger

ShadowOfTheBeast said:
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
 
G

Guest

Thanks Max, i appreciate it i will try it now

Markus Stoeger said:
ShadowOfTheBeast said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top