Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here the example.
Starts with : imgurl=
Ends with : &
Example extraction
asfasfasfasimgurl=http://www.mysite.com&asgasgas
Result: http://www.mysite.com
So how can I write regex to extract all instances like this?
You can use lookbehind and lookahead
(?<=imgurl=).*?(?=&)
Lookahead and lookbehind
Greedy Quantifiers
You can get a list of urls using
List<String> urls=Regex.Matches(input,regex)
.Cast<Match>()
.Select(x=>x.Value)
.ToList();
A simple regex could be:
(?:imgurl=)(.*)(?:&)
the (?:[stuff here]) is a non-capture group. It requires the pattern to match, but not capture/extract. The (.*) captures everything in-between the two non-capture groups.
Also to learn more about capture groups you can read here
What is a non-capturing group? What does a question mark followed by a colon (?:) mean?
Related
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
Any idea what is the best way to check if a string contains a value with following pattern and extract that value:
ana-aa-aaaa
Above a is Alpha and n is numeric.
You can use it with a simple regular expression:
Regex.Match(value, #"\p{L}\d\p{L}-\p{L}{2}-\p{L}{4}")
This produces a Match object (which may or may not be a success). Getting the value of a successful match will produce the desired substring.
Above, \p{L} matches a letter, \d matches a decimal digit, and - matches a dash. When followed by {<number>}, an expression requires a specific number of matches.
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 want regx pattern in C# which find substring in any string which comes in middle only. Let say ,
Input : "toprohitpop rohittoppop toppoprohit"
find substring : "rohit"
Replace with : "$$$$"
Output : "top$$$$pop rohittoppop toppoprohit"
if substring "rohit" comes in left or right of the string then it should not be replaced.Substring "rohit" will only be replaced when it comes in middle of string .
Thanks in advance.
Use non-word-break anchors:
\Brohit\B
The \B will only match if it is in the middle of a word.
Read about it.
var input = "toprohitpop rohittoppop toppoprohit";
var regex = new Regex(#"\Brohit\B");
var output = regex.Replace(input, "$$$$$$$$");
See "Anchors" in Regular Expression Language.
Also, be careful with the '$' in the substitution string (see comments)
Use the following regex: .+rohit.+
Basically it enforeces at least one char before rohit and one after
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 am trying to create a RegEx and C# pattern that will match a phrase like:
Photos of Washington DC taken by Jane Doe
Where the capture groups result in "Photos" "Washington DC" and "Jane Doe". Other possibilities would be:
Videos of Austin taken by Ruby : Videos, Austin, Ruby
Photos of Red Bud Dogs taken by Willa Shepherd :Photos, Red Bud Dogs, Willa Shepherd
Is this even possible with RegEx?
It appears that I got flagged...did I mention that I don't know RegEx?
I tried: (Photo of).*?((?:[a-z][a-z]+))(Taken by)((?:[a-z][a-z]+)) but that failed.
.* matches any string (except newlines). By adding a ? to it (.*?), you can tell the regex engine to match as few characters as possible, which is probably the right approach here, so the very first instances of of and taken by will be used as separators of your intended sub-matches:
matchResults = Regex.Match(subjectString, "^(.*?) of (.*?) taken by (.*)");
// matchResults.Groups[1].Value contains "Photos" etc.
If you don't expect more than one of and taken by in your input, you can change all .*? into .*.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to make regular expression that allow only numbers and must be mandatory. no blank field or white space allow using asp regular expression validater with C#. i tried this
ValidationExpression="^[/d]*+$". this is working for number only. blank field accepted by this expression.
Thanks
Be careful that \d can match digits other than 0 to 9, such as Eastern Arabic numerals. I would suggest using:
"^[0-9]+$"
The RegexValidator isn't called when the field is empty, you must use the RequiredFieldValidator in combination : http://msdn.microsoft.com/en-us/library/eahwtc9e%28v=vs.100%29.aspx
Besides, the correct regex would be
^\d+$
\d is for any number
+ is for 1 to n occurences
Take out the "*". The "+" is being applied to the results of the previous expression which is "0 or more digits" and is always found.
ValidationExpression="^[/d]+$"
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 need to parse the email address from a mailto tag. I am looking for a way to do this via RegEx in C#.
Example input:
<mailto:abc#xyz.com>
Example output:
abc#xyz.com
In general, it's a very bad idea to use regular expressions for HTML parsing. Instead, take a look at the Html Agility Pack. For the specific input you provided, you may use:
(?<=\<mailto:).*(?=\>)
Here's a code sample:
var emailTag = "<mailto:abc#xyz.com>";
var emailValue = Regex.Match(emailTag, #"(?<=\<mailto:).*(?=\>)").Value;
Console.WriteLine(emailValue);
A simple Regex to strip anything in a mailto tag would be
<mailto:(.*?)>
You could use:
[\w\d]+\#[\w\d]+\.com
[\w\d] <----This matches any letter or character. \w matches any letter. \d matches anynumber.
+ <----One or more of previous item, in this case [\w\d]+ one or more letters or numbers
\# <----Simply matches the # symbol but it needs to be escaped with a \ as it is a special character
[\w\d]+ <----Same again
\. <---- Same concept as the # as . is a special character so it needs to be escaped
In your example:
[\w\d]+=abc
\#=#
[\w\d]+=xyz
\.=.
com=com
If your wanting to match special characters as well as letters and digits then just replace [\w\d]+ with [\S]+ (make sure s is capital).
[\S]+ <---Matches anything that is not a space.
You will have to do variations to include .co.uk and .org etc.
http://www.regular-expressions.info/reference.html <----This is very useful!