Can someone please explain how to get this regex working? I'm trying to take this string:
"Test0/1"
and turn it into:
"Test0\/1"
I'm using this, but it is not working:
var test = Regex.Replace("Test0/1", #"/", #"\/");
It keeps giving me
"Test0\\/1"
Then I want to take the results of the string and put it into a Regex statement like so:
var match = new Regex(test).Match(myString);
So the string 'test' has to be a valid regex statement.
Basically what I'm trying to do is take a list of interfaces off a device, create a regex statement out of them and then use that regex to compare results for other things in my code. Because of the way interfaces are formatted "FastEthernet0/1" for example, it is causing my regex to fail because you have to escape all forward slashes. I have to build this regex on the fly though because every device will have a different set of interfaces.
This is a function of Visual Studio automatically escaping the \ on your behalf. Look at the following question: What's the use/meaning of the # character in variable names in C#?. Removing the # symbol from #"\" turns the string into "\\".
Related
hey so my current regex is #"(into)(to)add\s[^\s]{1,}\1|\2[^\s]{1,}" I want the input to be something "add word into/to category" the regex in general works fine but just the \1|\2 part, I tried using groups and all sorts of solutions but I just can't seem to figure out how I can make it so that the input can be into or to
Can anyone help me out? (this is in C# and using the Regex class)
If I have understood you correctly, then you don't need back references to (unnamed) Groups, you can use a simple alternation, like this:
#"add \w+ (into|to) \w+"
That will select either into or to in the search string.
Edit:
Let's get a Little more 'advanced', using the optional sign '?':
#"add \w+ (in)?to \w+"
This will match 'in' zero or one time, followed by 'to', so it will match into as well as to, exactly as the original RegEx.
Edit2:
I have a feeling, you want to use a variable inside your RegEx, you can of course do that like this:
string search = "into|to";
RegEx regEx = new ReqEx(#"add \w+ (" + search + ") \w+");
From your given example I think you're looking for a regex like add\s\w+\s(into|to)\s\w+. Your current regex matches only strings starting with "intoto" wich is probably not what you want.
I'm trying to pull text out of a word document using regex look ahead and look behind foudn in this answer:
Regular Expression to find a string included between two characters while EXCLUDING the delimiters
The delimeters I have to work with are
Start: RQ
End: END-RQ
I have added the following (powershell) code:
$regex = [regex] '(?<=RQ)(.*?)(?=END-RQ)'
$matches = $regex.Matches($concat)
The problem is the matching is grabbing the RQ from END-RQ as the beginning of the next pattern. Can anyone tell me how to eliminate that (e.g. force the regex to match exactly RQ and END-RQ)? Wrapping the matching patterns in quotes does not seem to work, even when the quotes are escaped.
Try this:
$regex = [regex] '(?<=(?<!END-)RQ)(.*?)(?=END-RQ)'
you should download this application:
http://www.sellsbrothers.com/posts/Details/12425
it is priceless when trying to debug regex.
This might work (hard to say without knowing exactly what your data is):
$regex = [regex]'(?<=(?:^|[^-])RQ)(.*?)(?=END-RQ)'
I have a Regex that I now need to moved into C#. I'm getting errors like this
Unrecognized escape sequence
I am using Regex.Escape -- but obviously incorrectly.
string pattern = Regex.Escape("^.*(?=.{7,})(?=.*[a-zA-Z])(?=.*(\d|[!##$%\?\(\)\*\&\^\-\+\=_])).*$");
hiddenRegex.Attributes.Add("value", pattern);
How is this correctly done?
The error you're getting is coming at compile time correct? That means C# compiler is not able to make sense of your string. Prepend # sign before the string and you should be fine. You don't need Regex.Escape.
See What's the # in front of a string in C#?
var pattern = new Regex(#"^.*(?=.{7,})(?=.*[a-zA-Z])(?=.*(\d|[!##$%\?\(\)\*\&\^\-\+\=_])).*$");
pattern.IsMatch("Your input string to test the pattern against");
The error you are getting is due to the fact that your string contains invalid escape sequences (e.g. \d). To fix this, either escape the backslashes manually or write a verbatim string literal instead:
string pattern = #"^.*(?=.{7,})(?=.*[a-zA-Z])(?=.*(\d|[!##$%\?\(\)\*\&\^\-\+\=_])).*$";
Regex.Escape would be used when you want to embed dynamic content to a regular expression, not when you want to construct a fixed regex. For example, you would use it here:
string name = "this comes from user input";
string pattern = string.Format("^{0}$", Regex.Escape(name));
You do this because name could very well include characters that have special meaning in a regex, such as dots or parentheses. When name is hardcoded (as in your example) you can escape those characters manually.
Say you have a string:
string s = "GameObject.Find(\"obj\").GetComponent(\"comp\").GetMethod(\"method\").Get...";
The string can have any number of GetX() methods appended to it.
And you need to separate each method without the "." separator. Although, GameObject.Find can keep the (dot).
Here is my code so far :
Match match = Regex.Match(s, "(.+?\\(\".+?\"\\))(?:\\.??)*");
This produces only one group. What is the correct solution to this problem?
Edit :
Updated with non-capturing group.
First I'd recommend using verbatim string literals for writing regular expressions in C#. This cuts down the number of backslashes you need to write.
#"(.+?\("".+?""\)\.??)*"
To get all the captures, inspect Match.Captures.
See it working online: ideone
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