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.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am new to regex in c# and i am trying to figure out a way to pull data from a user input string. So far I have tried to use the Regex.Matches and the Regex.Split but no matter what i try i can't seem to understand how to write my regular expression to find what i want. Here is the input string example:
-new -task:my task 1 -body:this is the body for task one -priority:1
i would like to split this so that i can get everything that is in between the :(colon) and the -
so for example, i would like for one of my matches/split results to be: my task 1
and then another match to be: this is the body for task oneand so on. Thank you
You can use Match in Csharp
string input = "-new - task:my task 1 - body:this is the body for task one -priority:1";
string pattern = #":(.*?)-";
Match match = Regex.Match(input, pattern);
while (match.Success)
{
Console.WriteLine(match.Groups[1].Value);
match = match.NextMatch();
}
This is probably parsable with Regex. But regex has the downside of, while being nifty. It has a heavy cognitive load to understand. Since this only very little information being inputted (and not a huge file with allot of cases you need to shift through)
And I believe you have full control over how the format is being inputted into the program. I'd advice you not to use regex. And just to a string.split in the '-'. And simply interpret each argument as you walk over the array.
This should be much easier to maintain in the long run. Because if you have to ask about the regex online now, think about what will happen if you have to maintain the code again in the future.
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 6 years ago.
I'm converting a lot of code from legacy to maintainable and I'm creating a list of regex we can use to do all the pages quickly and the same. My regex skills are that of a child running with a knife...its not great. I've looked up a lot of different ways to only find the first set but I can't seem to get it to work. Can anyone solve this specific problem for me?
Here is the regex search and replace I'm using.
regex: (rs.*)\.Fields\[\"(\w+)\"\].Value
replace: $1.GetValue<object>("$2")
Works
code to search: ...rsProducts.Fields["Price"].Value...
result: rsProducts.GetValue<object>("Price")
This, as I want it to, finds the rs (recordset) of something and changes the way that we extract the value to use an extension method.
Does Not Work
code to search: ...rsProducts.Fields["Price"].Value + rsProducts.Fields["Price2"].Value...
result: rsProducts.Fields["Price"].Value + rsProducts.Fields["Price2"].Value
should be: rsProducts.GetValue<object>("Price") + rsProducts.GetValue<object>("Price2")
In this case the search does match 2 distinct instances but instead it matches the entire line. Here's a pic from regexr.com.
// sorry I don't have the reputation to post the image as an image but heres the
Link to Example Image
You're not dealing handling the case for the + between the two.
(rs.*?)\.Fields\[\"(\w+)\"\].Value
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.
This question already has an answer here:
.NET Regex Negative Lookahead - what am I doing wrong?
(1 answer)
Closed 6 years ago.
I asked this previously and used what I believe to be entirely too simple of a construct, so I'm trying again...
Assuming that I have:
This is a random bit of information from 0 to 1.
This is a non-random bit of information I do NOT want to match
This is the end of this bit
This is a random bit of information from 0 to 1.
This is a non-random bit of information I do want to match
This is the end of this bit
And (attempting) the following regex:
/This is a random bit(?:(?!NOT).)*?This is the end/g
Why is this not matching?
Regexr.com link: http://regexr.com/3db8m
What I'm looking to accomplish:
1) Determine a match based on a partial string of a line
2) Determine a match that ends with a partial string of a line
3) NOT capture based on some random string inside that start/end of a match.
edit
The patterns suggested in the original question were entirely too complicated for my meager understanding of Regex. Further, the suggestion of (?s) was throwing errors on regexr.com (ERROR: Invalid target for quantifier), so I reconstructed the question here.
If, indeed, there is a method to edit a question once asked, I apologize for not finding the edit link. I did find this edit link, as this question was marked as a 'duplicate' and 'previously answered'.
Respectfully, an answer not understood is no answer. As the author and seeker of the information contained in both this, and my previous question, I state that Maximilian Gerhardt's answer was the more correct (for me, at least).
Also, no idea if this is what was expected of this edit? I usually resort to StackOverflow when I've left a large enough dent on my desk. If I'm mis-using the site, again, I apologize :)
Don't use . with text that has newlines in it..
Working example:
http://regexr.com/3db92
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.