d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Any Regex King Here ? > Paying Fgs For A Working Answer
Prev12
Add Reply New Topic New Poll
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Mar 31 2019 12:25pm
Quote (Klexmoo @ Mar 31 2019 05:04am)
That requires a C# parser, and cannot be done with regular expressions.
You can create a regular expression to do some of it, but it would always be possible to create another C# program that it doesn't highlight properly.

Since the compiler for C# (Roslyn) is open sourced by Microsoft, you can actually use that in your program to parse C# :) https://github.com/dotnet/roslyn/wiki/Getting-Started-C#-Syntax-Analysis or https://github.com/dotnet/roslyn


This is what I would do if you're making a command line tool. You could write the command line tool in C#/.NET and that will give you access to these APIs natively.
Member
Posts: 9,231
Joined: Jan 10 2012
Gold: 2,980.00
Mar 31 2019 08:33pm
Quote (Klexmoo @ Mar 31 2019 05:04am)
That requires a C# parser, and cannot be done with regular expressions.
You can create a regular expression to do some of it, but it would always be possible to create another C# program that it doesn't highlight properly.

Since the compiler for C# (Roslyn) is open sourced by Microsoft, you can actually use that in your program to parse C# :) https://github.com/dotnet/roslyn/wiki/Getting-Started-C#-Syntax-Analysis or https://github.com/dotnet/roslyn


It is most likely the best way to do what I want to do, but I'll have to check with my teacher if it's ok with him (or if he wanted us to explore Regex only). What he asked was to highlight keywords. Quotes and comments were bonuses... and I want to explore further and resolve bonuses lol.
Member
Posts: 12,703
Joined: May 17 2013
Gold: 12,935.00
Apr 1 2019 12:05pm
Quote (Access @ 1 Apr 2019 03:33)
It is most likely the best way to do what I want to do, but I'll have to check with my teacher if it's ok with him (or if he wanted us to explore Regex only). What he asked was to highlight keywords. Quotes and comments were bonuses... and I want to explore further and resolve bonuses lol.


You can't complete the assignment fully with quotes and comments with a regular expression. If your teacher says otherwise, tell him/her to read up on ambiguous grammars :thumbsup:

This post was edited by Klexmoo on Apr 1 2019 12:07pm
Member
Posts: 9,231
Joined: Jan 10 2012
Gold: 2,980.00
Apr 1 2019 04:40pm
Quote (Klexmoo @ Apr 1 2019 01:05pm)
You can't complete the assignment fully with quotes and comments with a regular expression. If your teacher says otherwise, tell him/her to read up on ambiguous grammars :thumbsup:


Oh I have no doubt he knows that, he's one of the 10 Canadians part of the C++ language committee, so he's far from being a dumbass haha. We can choose whatever technology we want (as long as it's a technology we never really looked into) and I just thought it was possible to complete 100% of the assignment with mostly Regex, but apparently it isn't haha.

This post was edited by Access on Apr 1 2019 04:44pm
Member
Posts: 12,703
Joined: May 17 2013
Gold: 12,935.00
Apr 2 2019 01:30am
I bet you will get major bonus points by using Roslyn for it then :)
Member
Posts: 2,903
Joined: Aug 25 2009
Gold: 170.00
Apr 4 2019 02:35pm
Code

/(("[^"]*")|(\/*.*?\*\/)|(\/\/.*)|(?<result>Whatyousearchfor))/gm


if the element 'result' is found, you found what you needed.

Keep in mind this does work with //'s and with /* */'s, and with double and single qoutes...
Depending on the engine, this might not work with multi line /* comment blocks.

Oh, i guess this is a bit redundant to say so, but "whatyousearchfor" can be a regex ofc ;)


But srsly dude, get a parser.
Member
Posts: 23,728
Joined: Aug 21 2007
Gold: 433.48
Trader: Trusted
Apr 26 2019 10:53am
so you have a word foo that you want to find within a given code in C#, but the word itself should not be within quotes or comments?

Does it need to be one regex? Or could you filter for results in multiple steps?

So if you want to find the word foo only the second example should be found?

Code

# 1 example
/*
foo
*/

# 2 example
foo

# 3 example
"foo"

# 4 example
//foo

# 5 example
/* foo
*/

# 6 example

/*foo*/

# 7 example
"foo
"
# 8 example
"
foo
"


If stuff like example 8 can be persistent within the code it is in my opinion not doable with just one regular expression as the word foo could be within the quotes or outside - the full context here matters
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll