I want to find exact url mach in url list using with Regular Expression .
string url = #"http://web/P02/Draw/V/Service.svc";
string myword = #"http://web/P02/Draw/V/Service.svc http://web/P02/Draw/V/Service.svc?wsdl";
string pattern = #"(^|\s)" + url + #"(\s|$)";
Match match = Regex.Match(pattern, myword);
if (match.Success)
{
myword = Regex.Replace(myword, pattern, "pattern");
}
But the pattern returns no result.
What do you think is the problem ?
Strange formatting aside, here is a pattern to match each individual URL in your list.
Pattern = "http://([a-zA-Z]|/|[0-9])*\.svc";
Frankly, I don't think you're having issues with syntax or implementation. If you want to tweak the expression I wrote above, this is the place to do it: Online RegEx Tool
You're passing wrong arguments to Regex.Match method. You need to swap arguments like this>
Match match = Regex.Match(myword,pattern);
Why not use Linq on the string collection (when splitted by a space)
myword.Split(' ').Where(x => x.Equals(url)).Single().Replace(url, "pattern");
You've got your arguments the wrong way around, as has been pointed out
. in a regular expression pattern is a special character, so you need to escape url when you use it to build pattern - you can use Regex.Escape(url)
You don't need to check the match is a success before performing the replacement, unless you have other logic that depends on whether the match was a success.
Related
I have a string and a regular expression that I am running against it. But instead of the first match, I am interested in the last match of the Regular Expression.
Is there a quick/easy way to do this?
I am currently using Regex.Matches, which returns a MatchCollection, but it doesn't accept any parameters that will help me, so I have to go through the collection and grab the last one. But it seems there should be an easier way to do this. Is there?
The .NET regex flavor allows you to search for matches from right to left instead of left to right. It's the only flavor I know of that offers such a feature. It's cleaner and more efficient than the traditional methods, such prefixing the regex with .*, or searching out all matches so you can take the last one.
To use it, pass this option when you call Match() (or other regex method):
RegexOptions.RightToLeft
More information can be found here.
Regex regex = new Regex("REGEX");
var v = regex.Match("YOUR TEXT");
string s = v.Groups[v.Count - 1].ToString();
You could use Linq LastOrDefault or Last to get the last match from MatchCollection.
var lastMatch = Regex.Matches(input,"hello")
.OfType<Match>()
.LastOrDefault();
Check this Demo
Or, as #jdweng mentioned in the comment, you could even access using index.
Match lastMatch = matches[matches.Count - 1];
I'm rather inexperienced in regex, but what I need to do is match a URL in order to route it correctly. Examples:
/2013/06/article-title
/2013/06
/2013
Are all possible paths I need to check for. I did some research and found a little bit about checking for an exact length. But when I tried to modify it for my own use, it returns false.
Here's what I had for the simplest:
^\\~/([0-9]{4})$
Any ideas? Thanks.
For reference, here's the code that tries to match it:
string url = HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath;
Regex r = new Regex(regexp, RegexOptions.IgnoreCase);
m = r.Match(url);
return m.Success;
you can use this with groups (?:..) and question marks that make groups optional :
^/[0-9]{4}(?:/[0-9]{2}(?:/[\w-]+)?)?$
Question: What's the simplest way how to test if given Regex matches whole string ?
An example:
E.g. given Regex re = new Regex("."); I want to test if given input string has only one character using this Regex re. How do I do that ?
In other words: I'm looking for method of class Regex that works similar to method matches() in class Matcher in Java ("Attempts to match the entire region against the pattern.").
Edit: This question is not about getting length of some string. The question is how to match whole strings with regular exprestions. The example used here is only for demonstration purposes (normally everybody would check the Length property to recognise one character strings).
If you are allowed to change the regular expression you should surround it by ^( ... )$. You can do this at runtime as follows:
string newRe = new Regex("^(" + re.ToString() + ")$");
The parentheses here are necessary to prevent creating a regular expression like ^a|ab$ which will not do what you want. This regular expression matches any string starting with a or any string ending in ab.
If you don't want to change the regular expression you can check Match.Value.Length == input.Length. This is the method that is used in ASP.NET regular expression validators. See my answer here for a fuller explanation.
Note this method can cause some curious issues that you should be aware of. The regular expression "a|ab" will match the string 'ab' but the value of the match will only be "a". So even though this regular expression could have matched the whole string, it did not. There is a warning about this in the documentation.
use an anchored pattern
Regex re = new Regex("^.$");
for testing string length i'd check the .Length property though (str.Length == 1) …
"b".Length == 1
is a much better candidate than
Regex.IsMatch("b", "^.$")
You add "start-of-string" and "end-of-string" anchors
^.$
In C#, how would I capture the integer value in the URL like:
/someBlah/a/3434/b/232/999.aspx
I need to get the 999 value from the above url.
The url HAS to have the /someBlah/ in it.
All other values like a/3434/b/232/ can be any character/number.
Do I have escape for the '/' ?
Try the following
var match = Regex.Match(url,"^http://.*someblah.*\/(\w+).aspx$");
if ( match.Success ) {
string name = match.Groups[1].Value;
}
You didn't specify what names could appear in front of the ASPX file. I took the simple approach of using the \w regex character which matches letters and digits. You can modify it as necessary to include other items.
You are effectively getting the file name without an extension.
Although you specifically asked for a regular expression, unless you are in a scenario where you really need to use one, I'd recommend that you use System.IO.Path.GetFileNameWithoutExtension:
Path.GetFileNameWithoutExtension(Context.Request.FilePath)
^(?:.+/)*(?:.+)?/someBlah/(?:.+/)*(.+)\.aspx$
This is a bit exhaustive, but it can handle scenarios where /someBlah/ does not have to be at the beginning of the string.
The ?: operator implies a non-capturing group, which may or may not be supported by your RegEx flavor.
Regex regex = new Regex("^http://.*someBlah.*/(\\d+).aspx$");
Match match = regex.Match(url);
int result;
if (match.Success)
{
int.TryParse(match.Groups[1].Value, out result);
}
Using \d rather than \w ensures that you only match digits, and unless the ignore case flag is set the capitalisation of someBlah must be correct.
I have the following text that I am trying to parse:
"user1#emailaddy1.com" <user1#emailaddy1.com>, "Jane Doe" <jane.doe# addyB.org>,
"joe#company.net" <joe#company.net>
I am using the following code to try and split up the string:
Dim groups As GroupCollection
Dim matches As MatchCollection
Dim regexp1 As New Regex("""(.*)"" <(.*)>")
matches = regexp1 .Matches(toNode.InnerText)
For Each match As Match In matches
groups = match.Groups
message.CompanyName = groups(1).Value
message.CompanyEmail = groups(2).Value
Next
But this regular expression is greedy and is grabbing the entire string up to the last quote after "joe#company.net". I'm having a hard time putting together an expression that will group this string into the two groups I'm looking for: Name (in the quotes) and E-Mail (in the angle brackets). Does anybody have any advice or suggestions for altering the regexp to get what I need?
Rather than rolling your own regular expression, I would do this:
string[] addresses = toNode.InnerText.Split(",");
foreach(string textAddress in addresses)
{
textAddress = address.Trim();
MailAddress address = new MailAddress(textAddress);
message.CompanyName = address.DisplayName;
message.CompanyEmail = address.Address;
}
While your regular expression may work for the few test cases that you have shown. Using the MailAddress class will probably be much more reliable in the long run.
How about """([^""]*)"" <([^>]*)>" for the regex? I.e. make explicit that the matched part won't include a quote/closing paren. You may also want to use a more restrictive character-range instead.
Not sure what regexp engine ASP.net is running but try the non-greedy variant by adding a ? in the regex.
Example regex
""(.*?)"" <(.*?)>
You need to specify that you want the minimal matched expression.
You can also replace (.*) pattern by more precise ones:
For example you could exclude the comma and the space...
Usually it's better to avoid using .* in a regular expression, because it reduces performance !
For example for the email, you can use a pattern like [\w-]+#([\w-]+.)+[\w-]+ or a more complex one.
You can find some good patterns on : http://regexlib.com/