i just want to match this string with regex:
How can i do this?
/profil
i try do this in this way:
.*/profil.*
But my software dont match any results in text.
All you need is this
#"(\/profil)"
if all you need is to match "/profil" then there is no need for the ".*"
Remember the expressions are greedy.
The . matches any character, and * tells it that this . can go on forever. So this expression will eat up all of your input, leaving nothing for the /profil part.
It seems like you're trying to put a wildcard around /profil. This is not needed with regular expressions. You should just be able to use /profil as the full expression and match your string.
Related
What I am trying to do is fairly simple, although I am running into difficulty. I have a string that is a url, it will have the format http://www.somedomain.com?id=someid what I want to retrive is the someid part. I figure I can use a regular expression but I'm not very good with them, this is what I tried:
Match match = Regex.Match(theString, #"*.?id=(/d.)");
I get a regex exception saying there was an error parsing the regex. The way I am reading this is "any number of characters" then the literal "?id=" followed "by any number of digits". I put the digits in a group so I could pull them out. I'm not sure what is wrong with this. If anyone could tell me what I'm doing wrong I would appreciated it, thanks!
No need for Regex. Just use built-in utilities.
string query = new Uri("http://www.somedomain.com?id=someid").Query;
var dict = HttpUtility.ParseQueryString(query);
var value = dict["id"]
You've got a couple of errors in your regex. Try this:
Match match = Regex.Match(theString, #".*\?id=(\d+)");
Specifically, I:
changed *. to .* (dot matches all non-newline chars and * means zero or more of the preceding)
added a an escape sequence before the ? because the question mark is a special charcter in regular expressions. It means zero or one of the preceding.
changed /d. to \d* (you had the slash going the wrong way and you used dot, which was explained above, instead of * which was also explained above)
Try
var match = RegEx.Match(theString, #".*\?id=(\d+)");
The error is probably due to preceding *. The * character in regex matches zero or more occurrences of previous character; so it cannot be the first character.
Probably a typo, but shortcut for digit is \d, not /d
. matches any character, you need to match one or more digits - so use a +
? is a special character, so it needs to be escaped.
So it becomes:
Match match = Regex.Match(theString, #".*\?id=(\d+)");
That being said, regex is not the best tool for this; use a proper query string parser or things will eventually become difficult to manage.
I need regular expression (C#) for symbol * - it should match any number of any characters, but it can contain only one space. I tried following, but its not working:
#".*[^[\t\0x0020]^[\t\0x0020]+].*"
#".*[^\s^\s+].*"
#".*[^\s\s+].*"
any way how to create regex like this?
Example: If user write expression MTN*-* it has to match for example
MTN3111-0000
but not
MTN311100 MTN3111-0000
You could use this expression:
\S*\s?\S*
It would match any number of any characters, but allow at most one space.
Here:
#"[^\s]*\s?[^\s]*"
Then there may be some specifics depending on other requirements
here try this:
([^\-]+\-[^\s]+)\s
I'm currently facing a (little) blocking issue. I'd like to replace a substring by one another using regular expression. But here is the trick : I suck at regex.
Regex.Replace(contenu, "Request.ServerVariables("*"))",
"ServerVariables('test')");
Basically I'd like to replace whatever is between the " by "test". I tried ".{*}" as a pattern but it doesn't work.
Could you give me some tips, I'd appreciate it!
There are several issues you need to take care of.
You are using special characters in your regex (., parens, quotes) -- you need to escape these with a slash. And you need to escape the slashes with another slash as well because we 're in a C# string literal, unless you prefix the string with # in which case the escaping rules are different.
The expression to match "any number of whatever characters" is .*. In this case, you would want to match any number of non-quote characters, which is [^"]*.
In contrast to (1) above, the replacement string is not a regular expression so you don't want any slashes there.
You need to store the return value of the replace somewhere.
The end result is
var result = Regex.Replace(contenu,
#"Request\.ServerVariables\(""[^""]*""\)",
"Request.ServerVariables('test')");
Based purely on my knowledge of regex (and not how they are done in C#), the pattern you want is probably:
"[^"]*"
ie - match a " then match everything that's not a " then match another "
You may need to escape the double-quotes to make your regex-parser actually match on them... that's what I don't know about C#
Try to avoid where you can the '.*' in regex, you can usually find what you want to get by avoiding other characters, for example [^"]+ not quoted, or ([^)]+) not in parenthesis. So you may just want "([^"]+)" which should give you the whole thing in [0], then in [1] you'll find 'test'.
You could also just replace '"' with '' I think.
Taryn Easts regex includes the *. You should remove it, if it is just a placeholder for any value:
"[^"]"
BTW: You can test this regex with this cool editor: http://rubular.com/r/1MMtJNF3kM
Aloha,
I need to match page.aspx?cmd=Show&id= but not match http://random address/page.aspx?cmd=Show&id=
Right now I'm just doing a simple resting replace with strContent.Replace("page.aspx?cmd=Show&id=", "page.aspx/Show/"); but to avoid the above i think I need to move to regular expressions.
Edit: Sorry i was a little short on details The current replace works because I was not taking into account that the address being modified should only be relative addresses, not absolute. I need to catch all relative addresses that match page.aspx?cmd=Show&id= and convert that string into page.aspx/Show/.
The .StartsWith and ^ wont work because I an looking in a string that is a full html page. So I need to be able to convert:
<p>Hello, please click here.</p>
into
<p>Hello, please click here.</p>
but not convert
<p>Hello, please click here.</p>
I used an A tag for my example but I need to convert IMG tags as well.
Thanks!
I would suggest using ParseQueryString rather than a regular expression because this will work even if the parameters are in a different order.
Otherwise you can use string.StartsWith to test if there is a match.
If you want to use a regular expression you need to use ^ and also escape all the special characters in your string:
Regex regex = new Regex(#"^page\.aspx\?cmd=Show&id=");
If you don't want to escape the characters yourself you can get Regex.Escape to do it for you:
string urlToMatch = "page.aspx?cmd=Show&id=";
Regex regex = new Regex("^" + Regex.Escape(urlToMatch));
You can just use a ^ to look for start of line.
... Or you can be cool and use a negative lookbehind (?<!http://.*?/). ;)
See http://www.geekzilla.co.uk/View6BD88331-350D-429F-AB49-D18E90E0E705.htm
I have a string which has to be matching #"^[\w*$][\w\s-$]*((\d{1,})){0,1}$".
If it doesn't match this regular expression, I want the characters that do not match to be deleted from the string. How can I set this up?
s = Regex.Replace(s, #"^[^[\w*\$][\w\s-\$]*((\d{1,})){0,1}]$", "")
You probably want something like (but I am not sure of the actual question). Maybe you want to remove the whole regex if it does not match; that's not what the code below does:
s = Regex.Replace(s, #"^[^\w*\$]([\w*\$])[^\w*\$\s-]*([\w\s-\$]*).*$", "$1$2")
The idea is to interleave each wanted character blocks with list of forbidden characters and keep only those that you want. The end of your regex was a bit strange, so I simplified it.