How do I make this regex stop at the first match? [duplicate] - c#

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

Related

how can i extract a specific part of a string in c# with regex.match? [duplicate]

This question already has answers here:
Regex Match all characters between two strings
(16 answers)
Closed 3 years ago.
Though this is probably a duplicate, i haven't been able to make this work after reading similar questions. Looking for help please with this homework, i'm a newbie at programming.
I'm working on a program in C#. I have a text that contains for example this sentence: "My homework version V0.90 from". I need to extract "V0.90", and that may vary from anything between V0.90 to V2.00. It is always surrounded by "My homework version " and " from", i need to extract whatever is between that. This is what i've tried:
string RetRegMatch;
Match Match1 = Regex.Match(Friends[a], #"My homework version (.+) from", RegexOptions.IgnoreCase);
RetRegMatch = Match1.Value;
But i'm geeting as a result in Match1.Value this: "My homework version V0.90 from", and i only want the (+.) part to be in Match1.Value. That is, i would like to have in Match1.Value this: "V0.90". How can i do this?
The part you need should be in the capture group (the part you put between ()). Try accessing it with
Match1.Groups[1].Value

How to match 2 substrings in responce with regex in c# [duplicate]

This question already has answers here:
Returning only part of match from Regular Expression
(4 answers)
Closed 4 years ago.
I have a responce string
"c=2020&action=approvecomment&_wpnonce=7508ac918a' data-wp-lists='dim:the-comment-list:comment-2020:unapproved:e7e7d3:e7e7d3:new=approved"
Im trying to extract 2020 and 7508ac918a. I dont understand how I must use regex with substrings in C#, simple regex like
c=(\d+)&action=approvecomment&_wpnonce=(.*?)' .+new=approved.
In Regex, you can create match groups
They look like this (?.+?)
So your _wpconce part could become something like this (?.*?)
Then you can grab each group individually for example
Match result = myRegex.Match(someString);
soneOtherString = result.Groups["GROUPNAME"].Value;
I use Regex101 to build and test my regex. (Whoever made that site deserves a crown with shinny stones on it!! :)
https://regex101.com/
Hope this helps

.NET Regex Negative Lookahead - What am I doing wrong (round 2!) [duplicate]

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

Regex to extract only domain from sub-domains [duplicate]

This question already has answers here:
What is a regular expression which will match a valid domain name without a subdomain?
(23 answers)
Closed 7 years ago.
i will be using the expression with
Regex.Replace();
to replace the rest with "".
inputs:
http://therealzenstar.blogspot.fr
output:
blogspot.fr
Just to iterate on Jens' comment, we have to guess: What is your expected output when additional information appears, e.g. http://therealzenstar.blogspot.fr/somedata.html. Is it still blogspot.fr? Are such examples needed to be adresed?
You said you want to replace "everything else" with "". Replace() will replace everything that is matched with what you want. So, to replace it with "", you'd need to match everything that you do not want.It's possible, however, it's much easier to capture what you DO want and replace all the match with $1.
Assuming you always want only the domain.xx, even if more information appears. Something like this will work: ^(?:https?:\/\/)?[^\/\s]*\.([^.\s\/]*\.[^.\s\/]*)(?:$|\/.*), as seen: https://regex101.com/r/hN8iQ7/1
A problem arises if your domains also include those with multiple extensions. I.e. domain.co.uk. You'd need to adress them specifically (naming them), as it is very hard to generalize a way to distinguish these items.
^(?:https?:\/\/)?[^\/\s]*?\.([^.\s\/]*\.(?:co\.uk|[^.\s\/]*))(?:$|\/.*) - with .co.uk option added. https://regex101.com/r/hN8iQ7/2 .
yourregex.Replace(yourstring, "$1") may do what you need.

how to search and hilight text containing special characters using javascript/jquery/c# [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Full text search in HTML ignoring tags / &
I did lots of googling but didnt find any help.
I have a webbrowser control wchich has HTML body.Body contains data that includes special charactors also. I want to add search box wchich will search and hilight it in page .Text can include special characters like \,/,?,$,^,&,<,> .
how should i achieve this using jquery/javascript or c#?
Here's an answer I gave to a similar question:
https://stackoverflow.com/a/5887719/96100
However, window.find(), which the above answer relies on, is likely to be removed from browsers in the future and is not going to be replaced in the short term. That being the case, I've written a flexible search function for my Rangy library. Demo (with highlighting) here:
http://rangy.googlecode.com/svn/trunk/demos/textrange.html

Categories

Resources