Connecting Tech Pros Worldwide Forums | Help | Site Map

Regular Expression question

Natalia DeBow
Guest
 
Posts: n/a
#1: Nov 16 '05
Hi,

I am stuck trying to come up with a regular expression for the following
pattern:
A string that contains "/*" but that does not contain */ within it.
Basically I am searching for C-style multiline comments and would much
rather use Regex than strings.

Here is what I have, but it does not seem to work.
Match match = Regex.Match(textLine, @"\s*(/[*])(?<comment>(?![*]/).*)$");

There is something wrong with the fact that the match should succeed only if
the string "*/" is not found.

Any input would be greatly appreciated!

Thanks,

Natalia



LeeHACK[Jhin-Seok.Lee]
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Regular Expression question


Hi

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconspecifyingfullyqualifiedtypenames.ht

You can show in this msdn document

Maybe you need just "/"

Show that a documen

Thanks

_________________________
Have a Good Day
_________________________
Mail :leehack@korea.co
Mail2:Jhin-Seok.Lee@oecd.or
MSN :leehack@Korea.co
Tel. :33-1-7142-640
_________________________
http://leehack.sarang.net
sevenfifteen
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Regular Expression question


Maybe not what you want, but it may be better to test that is IS a match on the regex "/\*[.\s\S]"
but DOESN'T match "/\*[.\s\S]/\*"
sevenfifteen
Guest
 
Posts: n/a
#4: Nov 16 '05

re: Regular Expression question


Woul
@"/\*(?![\s\S.]*\*/)

Do what you want

Natalia DeBow
Guest
 
Posts: n/a
#5: Nov 16 '05

re: Regular Expression question




Hi sevenfifteen,

Thanks so much for replying to my message. Your suggestion worked
indeed. Thanks!

I am stuck here on another problem, trying to come up with a regular
expression for the following case:
a. if a substring "/*" is detected, there is no "*/" that would follow;
b. if a substring "/*" is detected, there is neither "//" nor "/" that
preceeds it.

Part a is working fine, but when I add part b to it, things become
broken once again.

I have tried a few reg. ex.'s but nothing seems to work.

Here is what I have tried so far:
@"\s*(?!//?.*)(/[*])(?!.*[*]/)(?<comment>.*)"
@"\s*(?!.*//?)(/[*])(?!.*[*]/)(?<comment>.*)"
@"\s*(?<!//?.*)(?<=/[*])(?!.*[*]/)(?<comment>.*)"

Not sure where my logic is wrong.

Any help would be greatly appreciated!

Thanks,
Natalia

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Justin Rogers
Guest
 
Posts: n/a
#6: Nov 16 '05

re: Regular Expression question


Trying to match comments in /* */ pairs appears to be what you are doing.
Based on the semantics of something like c# it requires more logic than what
you are using.

"(?ms)^(?!.*//.*).*/\\*(?<comments>.*)\\*/"

The above is a C# escaped string that will match comment structures
not preceded by line comments "//".


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers


"Natalia DeBow" <natalia.debow@unisys.com> wrote in message
news:e9kEdEOSEHA.1276@TK2MSFTNGP11.phx.gbl...[color=blue]
>
>
> Hi sevenfifteen,
>
> Thanks so much for replying to my message. Your suggestion worked
> indeed. Thanks!
>
> I am stuck here on another problem, trying to come up with a regular
> expression for the following case:
> a. if a substring "/*" is detected, there is no "*/" that would follow;
> b. if a substring "/*" is detected, there is neither "//" nor "/" that
> preceeds it.
>
> Part a is working fine, but when I add part b to it, things become
> broken once again.
>
> I have tried a few reg. ex.'s but nothing seems to work.
>
> Here is what I have tried so far:
> @"\s*(?!//?.*)(/[*])(?!.*[*]/)(?<comment>.*)"
> @"\s*(?!.*//?)(/[*])(?!.*[*]/)(?<comment>.*)"
> @"\s*(?<!//?.*)(?<=/[*])(?!.*[*]/)(?<comment>.*)"
>
> Not sure where my logic is wrong.
>
> Any help would be greatly appreciated!
>
> Thanks,
> Natalia
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it![/color]


Justin Rogers
Guest
 
Posts: n/a
#7: Nov 16 '05

re: Regular Expression question


"(?ms)^.*?(?<!//[^\\n]*)/\\*(?<comments>.*)\\*/"

The above is a little better from what I can tell. You may
also need to make (?<comments>.*?), so that you don't
eat past the end of a comment.


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Justin Rogers" <Justin@games4dotnet.com> wrote in message
news:%23$ifNdOSEHA.3944@tk2msftngp13.phx.gbl...[color=blue]
> Trying to match comments in /* */ pairs appears to be what you are doing.
> Based on the semantics of something like c# it requires more logic than what
> you are using.
>
> "(?ms)^(?!.*//.*).*/\\*(?<comments>.*)\\*/"
>
> The above is a C# escaped string that will match comment structures
> not preceded by line comments "//".
>
>
> --
> Justin Rogers
> DigiTec Web Consultants, LLC.
> Blog: http://weblogs.asp.net/justin_rogers
>
>
> "Natalia DeBow" <natalia.debow@unisys.com> wrote in message
> news:e9kEdEOSEHA.1276@TK2MSFTNGP11.phx.gbl...[color=green]
> >
> >
> > Hi sevenfifteen,
> >
> > Thanks so much for replying to my message. Your suggestion worked
> > indeed. Thanks!
> >
> > I am stuck here on another problem, trying to come up with a regular
> > expression for the following case:
> > a. if a substring "/*" is detected, there is no "*/" that would follow;
> > b. if a substring "/*" is detected, there is neither "//" nor "/" that
> > preceeds it.
> >
> > Part a is working fine, but when I add part b to it, things become
> > broken once again.
> >
> > I have tried a few reg. ex.'s but nothing seems to work.
> >
> > Here is what I have tried so far:
> > @"\s*(?!//?.*)(/[*])(?!.*[*]/)(?<comment>.*)"
> > @"\s*(?!.*//?)(/[*])(?!.*[*]/)(?<comment>.*)"
> > @"\s*(?<!//?.*)(?<=/[*])(?!.*[*]/)(?<comment>.*)"
> >
> > Not sure where my logic is wrong.
> >
> > Any help would be greatly appreciated!
> >
> > Thanks,
> > Natalia
> >
> > *** Sent via Developersdex http://www.developersdex.com ***
> > Don't just participate in USENET...get rewarded for it![/color]
>
>[/color]


Justin Rogers
Guest
 
Posts: n/a
#8: Nov 16 '05

re: Regular Expression question


Eck, this one matches single line and multi-line comments.

Regex regex = new Regex(
"(?ms)" +
"^.*?((?<lineComment>//)|/\\*)" +
"(?<comments>.*?)" +
"(?(lineComment)$|\\*/)");

You can tell which you are matching by checking the lineComment
group for a value.


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Justin Rogers" <Justin@games4dotnet.com> wrote in message
news:uRbPX1OSEHA.3504@TK2MSFTNGP09.phx.gbl...[color=blue]
> "(?ms)^.*?(?<!//[^\\n]*)/\\*(?<comments>.*)\\*/"
>
> The above is a little better from what I can tell. You may
> also need to make (?<comments>.*?), so that you don't
> eat past the end of a comment.
>
>
> --
> Justin Rogers
> DigiTec Web Consultants, LLC.
> Blog: http://weblogs.asp.net/justin_rogers
>
> "Justin Rogers" <Justin@games4dotnet.com> wrote in message
> news:%23$ifNdOSEHA.3944@tk2msftngp13.phx.gbl...[color=green]
> > Trying to match comments in /* */ pairs appears to be what you are doing.
> > Based on the semantics of something like c# it requires more logic than what
> > you are using.
> >
> > "(?ms)^(?!.*//.*).*/\\*(?<comments>.*)\\*/"
> >
> > The above is a C# escaped string that will match comment structures
> > not preceded by line comments "//".
> >
> >
> > --
> > Justin Rogers
> > DigiTec Web Consultants, LLC.
> > Blog: http://weblogs.asp.net/justin_rogers
> >
> >
> > "Natalia DeBow" <natalia.debow@unisys.com> wrote in message
> > news:e9kEdEOSEHA.1276@TK2MSFTNGP11.phx.gbl...[color=darkred]
> > >
> > >
> > > Hi sevenfifteen,
> > >
> > > Thanks so much for replying to my message. Your suggestion worked
> > > indeed. Thanks!
> > >
> > > I am stuck here on another problem, trying to come up with a regular
> > > expression for the following case:
> > > a. if a substring "/*" is detected, there is no "*/" that would follow;
> > > b. if a substring "/*" is detected, there is neither "//" nor "/" that
> > > preceeds it.
> > >
> > > Part a is working fine, but when I add part b to it, things become
> > > broken once again.
> > >
> > > I have tried a few reg. ex.'s but nothing seems to work.
> > >
> > > Here is what I have tried so far:
> > > @"\s*(?!//?.*)(/[*])(?!.*[*]/)(?<comment>.*)"
> > > @"\s*(?!.*//?)(/[*])(?!.*[*]/)(?<comment>.*)"
> > > @"\s*(?<!//?.*)(?<=/[*])(?!.*[*]/)(?<comment>.*)"
> > >
> > > Not sure where my logic is wrong.
> > >
> > > Any help would be greatly appreciated!
> > >
> > > Thanks,
> > > Natalia
> > >
> > > *** Sent via Developersdex http://www.developersdex.com ***
> > > Don't just participate in USENET...get rewarded for it![/color]
> >
> >[/color]
>
>[/color]


Natalia DeBow
Guest
 
Posts: n/a
#9: Nov 16 '05

re: Regular Expression question


Hi there,

Thanks so much, Justin, for your very helpful comments. They are
greatly appreciated!

I have another question for .NET RegEx experts.

I am reading in a C Sharp file line by line and I am trying to detect
comments that start with either // of ///. What I am particularly
interested is the comments themselves. I am interested in some stats in
regards to the amount of comments in the file (comment bytes).

So, I tried several regular expressions, but they don't seem to work in
all the cases.

Here are the cases that I need to cover:

a. /// comments or // comments
b. /// <xml-tag> comments </xml-tag>
c. /// <xml-tag> comments <another xml-tag> comments </another xml-tag>
comments </xml-tag>
d. /// <xml-tag>
e. /// </xml-tag>

I need to be able to capture the comments and not the xml tags.

Here are a few of regular expressions that I have tried but
unsuccessfully.

@"^.*?///?\s*((</?.+>)*(?<comments>.*))*$"
@"///?\s*(</?.+>)*(?<comments>.*)"

I am having difficulty capturing multiple comments if they are separated
by xml tags. For some odd reason, if I have more than one set of tags,
the returned result is always the right most set of comments.

Thanks so much for any input!
Natalia




*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Closed Thread