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.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
In a string like
[\\\x2286400000\\\x22,\\\x22604800000\\\x22,2.0]\\n,null,1]\\n\x22,\x22mnr_crd\x22:\x221\x22,\x22msg_dsc\x22:\x22From your Internet address\x22,\x22msg_dvl\x22:\x22Reported by this computer\x22,\x22msg_err\x22:\x22Location unavailable\x22,\x22msg_gps\x22:\x22Using GPS\x22,\x22msg_unk\x22:\x22Unknown\x22,\x22msg_upd\x22:\x22Update location\x22,\x22msg_use\x22:\x22Use precise location\x22,\x22uul_text\x22:\x22Home Location\x22}
I am trying to match Home location with the regex like
(?<=:\\x22Use precise location\\x22,\\x22uul_text\\x22:\\x22)(.+?)(?=\\x22})
Here is the whole code:
string locationRegExpression = "(?<=:\\x22Use precise location\\x22,\\x22uul_text\\x22:\\x22)(.+?)(?=\\x22})";
Regex locationMmatch = new Regex(locationRegExpression, RegexOptions.Singleline);
MatchCollection locationCollection = Regex.Matches(locationHtmlContent,locationRegExpression);
// lblCurrentLocation.Text = "Location: " + locationCollection[0];
MessageBox.Show(locationCollection[0].ToString());
The above regex code is working fine with the below html code in online regex tester sites but if I use the same regex in C# win forms. Its giving 0 results. Any idea?
Whole text here.
It seems you want to match a single substring in a string containing literal \x22 substrings. You need to make sure you match a literal \ symbol, that is, you need to use two literal backslashes in your pattern. It is better done with a verbatim string literal (to avoid overescaping, use #"...") and it is enough to use a Regex.Match method:
string locationRegExpression = #"(?<=:\\x22Use precise location\\x22,\\x22uul_text\\x22:\\x22)(.+?)(?=\\x22})";
Regex locationMmatch = new Regex(locationRegExpression, RegexOptions.Singleline);
Match locationMatch = locationMmatch.Match(locationHtmlContent);
if (locationMatch.Success)
{
MessageBox.Show(locationMatch.Value);
}
Note that it might probably be "simpler" to use a capturing group here rather than a combination of lookbehind and lookahead:
#":\\x22Use precise location\\x22,\\x22uul_text\\x22:\\x22(.+?)\\x22}"
and then
MessageBox.Show(locationMatch.Groups[1].Value)
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.
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 6 years ago.
Improve this question
need to get rid of "organization" within a string with regex, the problem is organization is followed by different text for example it could be
Hosted/Organizationminor/
or
Hosted/Organizationmajor/
result for both should be
Hosted//
The following will find instances of "Organization" followed by any number of non-"/" characters, provided that "Organization" is preceded by "/" and that the whole thing is followed by "/". I'm using zero-width look-behind and look-ahead assertions, so that you can drop this in as the Regex and substitute it with an empty string.
(?=/)Organization[^/]*(?=</)
There is a non-regex solution with LINQ.
Since the task is to remove a known string "Organization" followed with any chars other than / after a / you can split the string with / and just rebuild the string using LINQ to only select the necessary parts:
string.Join("/", s.Split('/')
.Select(m => m.StartsWith("Organization") ? "" : m) // Empty the parts starting with the known word
);
See the IDEONE demo
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 7 years ago.
Improve this question
I need to extract from a string all the hashtags (#hashtag), mentions (#user) and links.
Right now I'm using this one:
#"((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+#)?[A-Za-z0-9.-]+|(?:www.|#|#|[-;:&=\+\$,\w]+#)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%#.\w_]*)#?(?:[\w]*))?)";
But it doesn't recognize users that starts with _ like "#_me" and links like this one (https://blogs.windows.com/windowsexperience/2015/12/03/whats-new-for-windows-10-iot-this-fall/#.VmB1q2NPg2A.twitter) are recognized partially.
How can I improve my regex to get all the possible cases?
Try this pattern (remember to turn RegexOptions.IgnorePatternWhitespace option on):
(?'tag'(#|\#)(\w|_)+)
|
(?'link'((https?://)|(www\.))[\w$-_.+!*'(),]+)
For this string:
My name is #_dave from #chicago. Visit my city at www.choosechicago.com/things-to-do/ Have a nice day!
It makes 3 captures: 2 under the tag group (#_dave and #chicago) and one under the link group (www.choosechicago.com/things-to-do/).
You can check it with a regex tester like Regex Storm
Explanation
RegexOptions.IgnorePatternWhitespace allows you to break your pattern into multiple lines for easier readability. Instead of this:
(?'tag'(#|#)(\w|_)+)|(?'link'www\.[\w$-_.+!*'(),]+)
You can write this when you turn on the option:
(?'tag'(#|\#)(\w|_)+) # capture # and # tags into the tag group
|
(?'link'www\.[\w$-_.+!*'(),]+) # capture hyperlinks, must begin with www
(?'tag'...) defines a capture group named tag, so you can refer to it by name Groups["tag"]rather by its positional value Groups[1].
[\w$-_.+!*'(),]+ defines the list of characters allowed in a URL, which I got from this question. I haven't checked the RFC specs so don't burn me if I missed a few.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string like this
ABCD$-$ToBeFetched1/$-$ToBeFetched2/$-EF$GH
How do I retrieve the string between $ and /$?
Expected string should be
ToBeFetched1
ToBeFetched2
Regex r = new Regex(Regex.Escape("-$") + "(.*?)" + Regex.Escape(#"/$"));
MatchCollection matches = r.Matches("ABCD$-$ToBeFetched1/$-$ToBeFetched2/$-EF$GH");
foreach (Match match in matches)
{
Console.WriteLine(match.Groups[1].Value);
}
Here this will work for sure.
Since you have "open" and "close" markers, the regex expression would obviously be built around this form:
[head-marker: $] [the content you are interested in: anything] [tail-marker: /$]
so, with addition of a parentheses to form a capturing group:
$(.*)$
However, two problems here: * expressions are greedy (and you dont want it to be, as you want all smallest matches possible) - so it has to be weakened, and also the $ is a special character in regex, so it has to be escaped:
\$(.*?)/\$
this forms almost-good expression. It will however falsely match agains such input:
aaaaa/$bbbbb/$ccccc -> bbbbb
because the "head-marker" can skip the slash and hit the first dollar sign what most probably you wouldn't want. Hence, some lookbehind would be useful here too:
(?!</)\$(.*?)/\$
The ?!<XXXX instructs to match only if XXXX does not precede the potential match.
See also MSDN: Regex syntax and operators
edit: actually Arie's suggestion is much simplier as it doesn't use capturing group. Note the small difference though: Arie's example explicitely fobids the data to contain a dollar sign, so ABCD$-$ToBeFe$tched1/$- will result in tched1 not ToBeFe$tched1. If you need the latter, just change the inner [^$] part. Think and pick what you actually need!
Using String Methods:
string s ="ABCD$-$ToBeFetched1/$-$ToBeFetched2/$-EF$GH";
var results = s.Split('-')
.Where(x=> x.StartsWith("$") && x.EndsWith("/$"))
.Select(y=> y.Substring(1,y.Length - 3));
//Console.WriteLine("string1: {0}, string2:{1}",results.ToArray());
(?<=\$)[^$]{1,}(?=/\$)
(?<=\$) - positive lookbehind: it ensurers your match starts right after $ ($ not included in the match)
[^$]{1,} - matches characters other than $; {1,} instead of * to ensure there will be no empty matches (for strings lilke "$/$")
(?=/\$) - positive lookahead: it ensures your match ends right before /$ (/$ not included in the match)