473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Check if a character is inside a source code string

Hi

I am working on an application where I need to parse C# source code. For
example, I need to find the return statement in a method. So I search for
the word "return" in a line of code. But the problem is when the word
"return" also is found in a string (either normal or literal).

Example:
.....
if (a == 2) return true; // This should give a match
if(s == "a string with return") b = 3; // This should not give a match

So I am looking for a way (using regex?) to determine if a position in a
string (e.g. char at index 5) is inside a string or not. Have tried to come
up with something without any luck.

Any ideas would be much appreciated.

Jul 21 '05 #1
1 1302
Hi mats

I'm not good with regex but if performance is not an issue.... you could
itenerate trought all the code one character at a time and keep track of any
quotes( " or ' ) that come up... so you keep a counter of them..

if you find one increase the counter by 1.... if it finds another one of the
same type( " or ' ).... decrease the counter.... of its of the other type
increase the counter....

so you only pickup the strings ( in this case "return" ) when the counter is
zero.

Hope that helps

"Mats Bue" <ma*****@broadpark.no> wrote in message
news:42********@news.broadpark.no...
Hi

I am working on an application where I need to parse C# source code. For
example, I need to find the return statement in a method. So I search for
the word "return" in a line of code. But the problem is when the word
"return" also is found in a string (either normal or literal).

Example:
....
if (a == 2) return true; // This should give a match
if(s == "a string with return") b = 3; // This should not give a match

So I am looking for a way (using regex?) to determine if a position in a
string (e.g. char at index 5) is inside a string or not. Have tried to
come up with something without any luck.

Any ideas would be much appreciated.

Jul 21 '05 #2

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

Similar topics

8
by: Greg Bryant | last post by:
I'm working on validating US phone numbers. I have a nice expression that Regex Coach likes, but causes PHP to reject everything I send. Are there any glaring differences? I can't figure out...
27
by: John Roth | last post by:
PEP 263 is marked finished in the PEP index, however I haven't seen the specified Phase 2 in the list of changes for 2.4 which is when I expected it. Did phase 2 get cancelled, or is it just not...
5
by: PEK | last post by:
I need some code that convert a multi-byte string to a Unicode string, and Unicode to multi-byte. I work mostly in Windows and know how to solve it there, but I would like to have some platform...
7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
1
by: Mats Bue | last post by:
Hi I am working on an application where I need to parse C# source code. For example, I need to find the return statement in a method. So I search for the word "return" in a line of code. But the...
9
by: Velvet | last post by:
I'm trying to convert some JavaScript to C# and don't know how to get the character code of a character in a string. in JavaScript it is as follows: for( i = 0; i < email.length; i++) { ...
15
by: wizardyhnr | last post by:
i want to try ANSI C99's unicode fuctions. so i write a test program. the function is simple, but i cannot compile it with dev c++ 4.9.9.2 under windows xp sp2, since the compiler always think that...
14
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
3
KevinADC
by: KevinADC | last post by:
Purpose The purpose of this article is to discuss the difference between characters inside a character class and outside a character class and some special characters inside a character class....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.