Implementing a simple C# code parser [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
My program should find Console.WriteLine(" in the source text box and start reading the string just after the " until it finds a "), and then store the captured string in a variable. For example, if the input is:
Console.WriteLine("Hello World")
Then the variable's value should be Hello World.
Help would be appreciated.

string yourInput = ...
var result = Regex.Match(yourInput, "Console.WriteLine[(]\"(.*?)(?<!\\\\)\"[)]").Groups[1].Value;
A little test:
string yourInput = "Console.WriteLine(\"\\\") Yeahhh\")";
yourInput += "Console.WriteLine(\"At least Regex can handle \"this\"\")";
yourInput += "Console.WriteLine(\"Although \"Regex\" is afraid of parsing \"text\" with nested elements\")";
var matches = Regex.Matches(yourInput, "Console.WriteLine[(]\"(.*?)(?<!\\\\)\"[)]");
foreach (Match m in matches)
System.Diagnostics.Debug.Print(m.Groups[1].Value);
Output
\") Yeahhh
At least Regex can handle "this"
Although "Regex" is afraid of parsing "text" with nested elements

Related

How to negate whole regex [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I have a pattern (pattern) and I want to exclude this characters in string 'test' and find all holes in this expression. Like test, but this characters will be random
For the string 'test' I expect 'test' which is not matched by pattern ()
You could use Regex#Match here:
Regex regex = new Regex(#"(XZ|RP(?!ROD)|R|DP(?!ROD)|D|ST[\d]?|WO)");
Match match = regex.Match("testRPRODRRPWO");
if (!match.Success)
{
Console.WriteLine("input does not contain pattern");
}

Find a word between 2 words and replace them [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How to find between 2 words and replace them into new string?
I wanna replace same.
for example given string :
string s = "word1-word2same-word3-word4";
same is = "word2";
What i wanna :
string s = "word1-word2word2-word3-word4";
But what if given string is:
string s = "word1same-word2-word3-word4";
same is -> word1;
What i wanna in this example:
string s = "word1word1-word2-word3-word4";
How to find what word is containing same? How to do it?
I think the following regular expression replacement is what you want
var input = "word1same-word2same-word3";
var result = Regex.Replace(input, "(?<=^|-)([^-]*)same", "$1$1");
Console.WriteLine(result);
would give you
word1word1-word2word2-word3
So it will replace all the instances of "same" with whatever comes before it up to a hyphen or the beginning of the string.
Note that something like "whatsamesamesame" will give you a result of "whatsamesamewhatsamesame", basically it will only replace the last "same" after a hyphen or the beginning of the string.

Searching and Extracting substring recursively with RegEx [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm wondering if there is a RegEx pattern to solve my problem.
I'm getting strings like:
"Running script A23jddie392.sql",
"Skipped script ew223.sql",
"Script 2234ffss321.sql has an error"
and so on.
Is it possible to extract the scriptname with RegEx? Maybe searching for the .sql and then going recursively to the first blank before the scriptname?
Thank you!
Here is the code:
var pattern = "\\s\\w+[.]sql";
var test = "Script 2234ffss321.sql has an error";
var match = Regex.Match(test, pattern);
if (match.Success)
{
Console.WriteLine(match.Groups[0].Value);
}

C# Regex to split string by commas that are only before "or" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to split the following string based on the commas that come before the "or" delimiter and on the "or", but not after. For example
Almondmilk, Coconutmilk or Soymilk Select Varieties, Half Gallon
becomes
Almondmilk
Coconutmilk
Soymilk Select Varieties, Half Gallon
Given your requirement as described:
var output = Regex.Split(input, "(?<!or.*),");
However, given your sample output it seems you want to split on 'or' as well:
var output = Regex.Split(input,
"((?<!or.*),)|(or)",
RegexOptions.ExplicitCapture);

"(" and ")" Replace in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
So I am trying to replace the characters below from a users input which relates to a SQL column in a table.
Example:
User creates a field called My(first)try. In SQL this comes up as Myfirsttry.
I am currently write code to replace the users input with our and I am stuck.
I have this so far.
itemreplace = itemreplace.Replace("(", "");
This however, doesn't do the trick. Thoughts, comments, suggestions?
I feel like the real-world case is more complicated, and you're not indicating that, but to handle your example text My(first)try you could just chain the Replace statements:
itemreplace = itemreplace.Replace("(", "").Replace(")", "");
However, it seems like the real-world case is more along the lines of leveraging a Regex like this:
^[a-zA-Z0-9_##][a-zA-Z0-9#$#_]*$
and here is a Regex 101 that would prove that.
Then using that might look like this:
var valid = Regex.IsMatch("My(first)try", pattern);
I did reference this post, What special characters are allowed in T-SQL column name?, to determine the allowed characters for a column name.
First option:
String itemreplace = new String("My(first)try");
String charsToRemove = new String[] {"(", ")"};
foreach (char c in charsToRemove)
{
itemreplace = itemreplace.Replace(c, string.Empty);
}
Second option:
itemreplace = itemreplace.Replace("(", string.Empty).Replace(")", string.Empty);

Categories

Resources