Matching a comment (//Comment) in regex outside quotation marks [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regex to strip line comments from C#
I'm completely stuck with this, and i'm not good at making regex.
Basicly i want to match comments in pieces of text, for example this one:
//Comment outside quotations
string text = "//Comment inside quotations..";
//Another comment
I want only the top and bottom comment to match, but not the middle one inside quotations
What i have now for comments is:
//.*$
To match a comment throughout the end of the line.
What i want this to use for is for syntax highlighting in a textBox.
Is this possible to do?

Try this :
"^(?!\".*\")//.*$"
This will match
//Comment outside quotations
and will not match
string text = "//Comment inside quotations..";
Please make required escaping for c#

Try this regex:
([^"]|"[^"]*")*(?<COMMENT>//.*)
Parse each match for the named group "COMMENT" (or whatever you choose to name it). Quick disclaimer that I didn't test it out in C#, I just threw the regex together using an online tool.

Related

Replace a string using regex; [duplicate]

This question already has answers here:
Regex.Match whole words
(4 answers)
Closed 4 years ago.
I have a xml file with some info. I need to replace some words from that with another word.
In the below example, I need to remove the "go" keyword with "C#":
Something like I'm going to have section on go language
For that I have wrote a regex like:
string reg = "[^a-zA-Z09]go[^a-zA-Z09]";
var r = new Regex(reg);
r.Replace("Something like I'm going to have section on go language", "C#");
//The expected result is Something like I'm going to have section on C# language
But the issue is, it's replacing with the space around the " go ".
If I use string replace function, it will remove go from going as well.
Try using \b in your regular expression: this is used for matching word boundaries.
string reg = #"\bgo\b";
Test that here.

Find combination of words within sentence [duplicate]

This question already has answers here:
Regex: I want this AND that AND that... in any order
(7 answers)
What is a regular expression for parsing out individual sentences?
(6 answers)
Closed 4 years ago.
I am trying to construct a regex patter that enables me to check if a specific combination of words appears within a sentence.
Text Example
In the body of your question, start by expanding on the summary you
put in the title. Explain how you encountered the problem you're
trying to solve, and any difficulties that have prevented you from
solving it yourself. The first paragraph in your question is the
second thing most readers will see, so make it as engaging and
informative as possible.
Now i am trying to create a pattern that will tell me if any sentence in this text contains a combination of words in any order.
Example combination:
summary, question
Example code:
Regex regex = new Regex(#"(summary|question).*\w\.");
Match match = regex.Match("In the body of your question, start by expanding on the summary you put in the title. Explain how you encountered the problem you're trying to solve, and any difficulties that have prevented you from solving it yourself. The first paragraph in your question is the second thing most readers will see, so make it as engaging and informative as possible.");
if (match.Success)
{
Console.WriteLine("Success");
} else {
Console.WRiteLine("Fail");
}
Output:
Success
Example Code:
Regex regex = new Regex(#"(summary|question).*\w\.");
Match match = regex.Match("Explain how you encountered the problem you're trying to solve, and any difficulties that have prevented you from solving it yourself. The first paragraph in your question is the second thing most readers will see, so make it as engaging and informative as possible.");
if (match.Success)
{
Console.WriteLine("Success");
} else {
Console.WRiteLine("Fail");
}
Output:
Fail
My ultimate goal is to read any number of words from user (1..n), construct them into regex pattern string and use that pattern to check against any text.
e.g. (please ignore the faulty pattern i am just using visual representation)
Words: question, summary
pattern: (question|summary).*\w
Words: user, new, start
pattern: (user|new|start).*\w
I really hope this makes sense. I am relearning regex (haven't used it in over decade).
EDIT 1 (REOPEN JUSTIFICATION):
I have reviewed some answers that were done previously and am little bit closer.
My new pattern is as follows:
/^(?=.*Reference)(?=.*Cheatsheet)(?=.*Help).*[\..]/gmi
But as per example here https://regex101.com/r/m2HSVq/1 it doesn't fully work. It looks for the word combination within the whole paragraph, rather than sentence.
As per original text, I want to only return match if within sentence (delimited by full stop or end of text).
My fallback option is to split string at full stops, then do individual matches if i can't find solution.

Best way to remove unknown characters and spaces using C#? [duplicate]

This question already has answers here:
How can I remove the spaces, tabs, new lines between characters using c#'s REGEX?
(2 answers)
Closed 6 years ago.
Unknown Characters:
|b9-12-2016,¢Xocoak¡LO2A35(2)(b)¡ÓocORe3ao-i|],¢Xa?u¡±o¡±i?¢X$3,597,669On 9-12-2016, the price adjusted to $3,597,669 dueto the reason allowed under section 35(2)(b) of theOrdinance
Good Result:
$3,597,669On 9-12-2016, the price adjusted to $3,597,669 due to the reason allowed under section 35 of the Ordinance
You should be able to use regular expressions to do this. You can use the Regex.Replace method to run regular expressions on your text. Regular expressions are patterns that a regular expression engine tries to match in input text. I recommend that you take a look at the MSDN article here. You can also take a look at the documentation for the Regex.Replace method here. For example, in order to remove the letter c you could use this snippet of code:
output = Regex.Replace(input, "c", "", RegexOptions.IgnoreCase);
This would replace both lowercase and capital Cs because the ignore case option is turned on.
If it is a standard pattern as what you've told me. Use the following code. It takes everything after the last $ sign.
string str = "|b9-12-2016,¢Xocoak¡LO2A35(2)(b)¡ÓocORe3ao-i|],¢Xa?u¡±o¡±i?¢X$3,597,669On 9-12-2016, the price adjusted to $3,597,669 dueto the reason allowed under section 35(2)(b) of theOrdinance";
var result = str.Substring(str.LastIndexOf('$'));

Need to get so substring but using Regular Expression [duplicate]

This question already has answers here:
C# Substring Alternative - Return rest of line within string after character
(5 answers)
Closed 6 years ago.
I got on url like.
http://EddyFox.com/x/xynua
Need to fetch substring after /x/ what ever string is there.
complex example I faced is :
http://EddyFox.com/x//x/
Here result should be /x/
It can be achieved with substring ,But we need to perform it with regular expression.
This should do it:
string s = "http://EddyFox.com/x/xynua";
// I guess you don't want the /x/ in your match ?=!
Console.WriteLine(Regex.Match(s, "/x/(.*)").Groups[1].Value );
this is probably even better:
Console.WriteLine(Regex.Match(s, "(?<=/x/)(.*)").Value );
the output is
xynua
Have a look at this post: Regex to match after specific characters SO is full of RegEx posts. The probability is very high that a RegEx question has already been asked before. :)
The regex /x/(.*) will capture everything following the /x/
And where is the problem?
var r = new Regex("/x/(\\S*)");
var matches = r.Matches(myUrl);
This regex matches everything from /x/ until the first occurence of a white-space.

Regex to match XML elements in a text file [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
RegEx match open tags except XHTML self-contained tags
I have a text file consist of conversion instruction templates.
I need to parse this text file,
I need to match something like this:
(Source: <element>)
And get the "element".
Or this pattern:
(Source: <element attr="name" value=""/>)
And get "element attr="name"".
I am currently using this regex:
\(Source:\ ?\<(.*?)\>\)
Sorry for being a newbie. :)
Thanks for all your help.
-JRC
Try this Regex for detect attibs by both ” or " characters:
\(Source:\s+<(\w+\s+(?:\w+=[\"”][^\"”]+[\"”])?)[^>]*>\)
and your code:
var result = Regex.Match(strInput,
"\\(Source:\\s+<(\\w+\\s+(?:\\w+=[\"”][^\"”]+[\"”])?)[^>]*>")
.Groups[1].Value;
explain:
(subexpression)
Captures the matched subexpression and assigns it a zero-based ordinal number.
?
Matches the previous element zero or one time.
\w
Matches any word character.
+
Matches the previous element one or more times.

Categories

Resources