d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Any Regex King Here ? > Paying Fgs For A Working Answer
12Next
Add Reply New Topic New Poll
Member
Posts: 9,231
Joined: Jan 10 2012
Gold: 2,980.00
Mar 29 2019 07:46pm
Hey guys ! I'm having a little issue with Regex. I'm in need of a Regex matching a specified word that is not contained in quotes (") , nor C# comments (// or /* */).

I have a base that works very well for ignoring the specified word in quotes, but I need to implement comments ignoring as well.

Here's the base I have :

Code
\b(?<=^([^\"\r\n]|\"([^\"\\\r\n*/]|\\.)*\")*)test\b


I tried implementing the // ignore, but I'm stuck. I tried something like this :

\b(?<=^([^\"\r\n/{2,}]|\"([^\"\\\r\n*/]|\\.)*\")*)test\b

but realized quickly that special characters are treated as literal characters in character sets [], so it is ignoring / { 2 , and } individually instead of ignoring anything following two front slashes or more.

I need a solution ASAP ! Paying 500 fgs for a working solution (ignoring // and /* */) !

This post was edited by Access on Mar 29 2019 07:47pm
Member
Posts: 12,703
Joined: May 17 2013
Gold: 12,935.00
Mar 30 2019 04:30am
What you need is a parser, not a regular expression.
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Mar 30 2019 02:11pm
edit: I'm stupid what is this for? Klexmoo is right and this isn't really doable.

This may be of help because Java and C# share comment syntax: https://stackoverflow.com/questions/1657066/java-regular-expression-finding-comments-in-code

This post was edited by waraholic on Mar 30 2019 02:16pm
Member
Posts: 12,703
Joined: May 17 2013
Gold: 12,935.00
Mar 30 2019 03:34pm
One thing you cando is create a regex that matches the quotes and all types of comments, which you use to remove that text. You can then do a search for the word you want on the text without the comments/quotes.

This post was edited by Klexmoo on Mar 30 2019 03:35pm
Member
Posts: 9,231
Joined: Jan 10 2012
Gold: 2,980.00
Mar 30 2019 05:00pm
Quote (waraholic @ Mar 30 2019 03:11pm)
edit: I'm stupid what is this for? Klexmoo is right and this isn't really doable.

This may be of help because Java and C# share comment syntax: https://stackoverflow.com/questions/1657066/java-regular-expression-finding-comments-in-code


I already have my independent Regex expressions (// and /* */) to find comments (and they work). The only problem is that the keyword Regex doesn't ignore comments. Thanks for the help though :)

Quote (Klexmoo @ Mar 30 2019 04:34pm)
One thing you cando is create a regex that matches the quotes and all types of comments, which you use to remove that text. You can then do a search for the word you want on the text without the comments/quotes.


This is not doable for my purpose (I think ?) because I need to replicate an IDE behavior (highlighting keywords, but not in comments or inside quotes, highlight text between quotes, as well as comments). So I need to keep comments to highlight them as well. It should also be a library independent code.
Member
Posts: 12,703
Joined: May 17 2013
Gold: 12,935.00
Mar 30 2019 05:02pm
Quote (Access @ 31 Mar 2019 00:00)
This is not doable for my purpose (I think ?) because I need to replicate an IDE behavior (highlighting keywords, but not in comments or inside quotes, highlight text between quotes, as well as comments). So I need to keep comments to highlight them as well. It should also be a library independent code.


Except that what you want to do is not parseable by regular expressions, because what you want to do is not regular.

IDE's implement a parser to be able to do this behavior, exactly because regular expressions are too limited to be able to do things like this.

This post was edited by Klexmoo on Mar 30 2019 05:03pm
Member
Posts: 12,703
Joined: May 17 2013
Gold: 12,935.00
Mar 30 2019 05:08pm
FYI: Read up on this https://blog.codinghorror.com/parsing-beyond-regex/ if you want to learn more about how this stuff works.

Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Mar 30 2019 07:08pm
In what context are you trying to highlight the word? In an IDE and if so which? In a browser? In a program you've written? There may be an alternative solution to the problem you're facing.
Member
Posts: 9,231
Joined: Jan 10 2012
Gold: 2,980.00
Mar 30 2019 08:16pm
Quote (Klexmoo @ Mar 30 2019 06:08pm)
FYI: Read up on this https://blog.codinghorror.com/parsing-beyond-regex/ if you want to learn more about how this stuff works.


I'll check that out thanks :)

Quote (waraholic @ Mar 30 2019 08:08pm)
In what context are you trying to highlight the word? In an IDE and if so which? In a browser? In a program you've written? There may be an alternative solution to the problem you're facing.


What we need to do is :

1) Write a command in console specifying (or not) optional parameters (-colors and/or -stats) and one or multiple .cs (C#) files
2) Read C# file(s) (source code) passed as arguments
3) If -stats was specified, count the number of word and number occurences
4) If -colors was specified, parse the source code and highlight what an IDE would (well only keywords, text between quotes and comments are required) by putting them into <span> tags with a color.
5) Create an HTML file including <span> tags highlighting keywords, text between quotes and comments.
Member
Posts: 12,703
Joined: May 17 2013
Gold: 12,935.00
Mar 31 2019 04:04am
Quote (Access @ 31 Mar 2019 03:16)
I'll check that out thanks :)



What we need to do is :

1) Write a command in console specifying (or not) optional parameters (-colors and/or -stats) and one or multiple .cs (C#) files
2) Read C# file(s) (source code) passed as arguments
3) If -stats was specified, count the number of word and number occurences
4) If -colors was specified, parse the source code and highlight what an IDE would (well only keywords, text between quotes and comments are required) by putting them into <span> tags with a color.
5) Create an HTML file including <span> tags highlighting keywords, text between quotes and comments.


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 post was edited by Klexmoo on Mar 31 2019 04:11am
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll