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
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 have silly problem with Asp.Net project that I'm working on it for more than 5 years.
Today suddenly the Trim() function stopped working.
Notes:
I update project framework from 4.7.2 to 4.8 and the problem still happen.
I tried TrimLeft() and TrimRight() also have the same problem of Trim()
Replace Function is still working fine not effected but it is not a good solution for me to use it every where.
Trim working fine on new projects
I also check the char code of space it is 32
any idea?
this is the code
string val = "abo "; // "abo\x0020\x0020\x0020\x0020\x200e"
string userName = val.Trim();
you can run the problem in this link
Update:
thanks you all for the comments, I also found a simple check to test the end of the char when you set the cursor at the end and press backspace one time nothing is happen the second press start deleting and that because of the \x200e char at the end.
any idea how to trim hidden Char from left and right and deal with just like spaces.
String.Trim works. If it didn't, hundreds of thousands of developers would have noticed 16 years ago.
The string ends with a formatting character, specifically \x200e, the Left-to-Right-Mark. That's definitely not a whitespace. Calling Char.GetUnicodeCategory('') returns Format. I suspect the input came from mixed Arabic and Latin text, perhaps something copied by a user from a longer string?
One way to handle this is to use String.Trim(char[]) specifying the LTR mark along with other characters. That's not quite the same as String.Trim() though, which removes any character that returns true with Char.IsWhiteSpace() :
var userName=val.Trim(' ','\t','\r','\n','\u200e`);
Another option would be to use a regular expression that trims both whitespace \s and characters in the Format Unicode category Cf, only from the start ^[\p{Cf}\s] or end ([\p{Cf}\s]+$) :
string userName = Regex.Replace(val,#"(^[\p{Cf}\s]+)|([\p{Cf}\s]+$)","");
Perhaps a better option would be to prevent unexpected characters using input validation, and require that the input TextBox contains only letter or letter and digit characters. After all, the user could paste some other unexpected non-printable character. It's better to warn the user than try to handle all possible bad data.
Usernames are typically letter and number combinations without whitespace. All ASP.NET stacks allow validation. Modern browsers allow regular expression validation in the input element directly, so we could come up with a regex that allows only valid characters, eg :
<input type="text" required pattern="[A-Z0-9]' ..../>
The NumberLetter block (Nl) could be used to capture numbers and letters in any language, just like Cf is used to capture format characters
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:
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.
This question already has answers here:
How can I deserialize JSON with C#?
(19 answers)
Closed 5 years ago.
Just a quick one, I'm probably just blind. I'm using regex to match bits of JSON data. I have this line:
String ErrorMessage = new Regex("\"message\":\"\\w+\"").Match(response).Value;
Which should hopefully match something like this:
"message":"This is a messsage"
But it isn't matching it at all, I know it's in there though.
Thanks in advance.
EDIT: While this might not be best practice, it's the first time that I've used both regex and JSON, and so didn't want to use any external APIs or libraries. I'll be sure to use something better in the future :)
This is not a duplicate, as I am not asking how to parse JSON, I'm simply asking why a regex doesn't match a pattern. Thanks.
Thanks for the answers!
The \w character class does not include the space character.
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