C# Regular Expression - Adding Special Characters - regex

I was wondering what is the best way to include backslash and other special characters in a group?
example:
"message":"\"rock on\" \\,,/,[-_-]";
help me on my regex
[a-zA-Z0-9 \\-~!##$%^*()_+{}:|?`;',\\./\\[\\]]+

Just escape those that need to be escaped and add those, that don't need to:
[a-zA-Z0-9 \\\-~!##$%^*()_+{}:|"?`;',./[\]]+
To elaborate a bit:
You only need to escape \, ] and - inside a character group.
Using C#, it would look like this:
Regex rx = new Regex(#"[a-zA-Z0-9 \\\-~!##$%^*()_+{}:|""?`;',./[\]]+");

Related

Which characters must be escaped in a Perl regex pattern

Im trying to find files that are looking like this:
access_log-20160101
access_log-20160304
...
with perl regex i came up with something like this:
/^access_log-\d{8}$/
But im not sure about the "_" and the "-". are these metacharacter?
What is the expression for this?
i read that "_" in regex is something like \w, but how do i use them in my exypression?
/^access\wlog-\d{8}$/ ?
Underscore (_) is not a metacharacter and does not need to be quoted (though it won't change anything if you quote it).
Hyphen (-) IS a metacharacter that defines the range between two symbols inside a bracketed character class. However, in this particular position, it will be interpreted verbatim and doesn't need quoting since it is not inside [] with a symbol on both sides.
You can use your regexp as is; hyphens (-) might need quoting if your format changes in future.
Your regex pattern is exactly right
Neither underscore _ nor hyphen - need to be escaped. Outside a square-bracketed character class, the twelve Perl regex metacharacters are
Brackets ( ) [ {
Quantifiers * + ?
Anchors ^ $
Alternator |
Wild character .
The escape itself \
and only these must be escaped
If the pattern of your file names doesn't vary from what you have shown then the pattern that you are using
^access_log-\d{8}$
is correct, unless you need to validate the date string
Within a character class like [A-F] you must escape the hyphen if you want it to be interpreted literally. As it stands, that class is the equivalent to [ABCDEF]. If you mean just the three characters A, - or F then [A\-F] will do what you want, but it is usual to put the hyphen at the start or end of the class list to make it unambiguous. [-AF] and [AF-] are the same as [A\-F] and rather more readable

Regex Everything except !"#$'*+,/:;\`|

I want every character possible (letters,numbers,everyting) except: !"#$'*+,/:;\|`
Is this correct? [^!"#$'*+,/:;\`|]
I made it work using http://www.regextester.com/ and your suggestions
*WORKS--> [^!"#$'*+,/:;\\`|]
A regex pattern to match a single \:
\\
It means you need to escape a backslash by adding another backslash to it. So, you need to use
[^!"#$'*+,/:;\\`|]
^^
Note that if you use it in a regular string literal in some programming language, you will have to double each of the backslashes (so that there are 4 backslashes).

Notepad++ regex replace - how to remove this string

I want to remove strings in the form of the following where some-text is a random text string.
$('#some-text').val();
I've tried various things but I think the $ sign is messing things up since it's used in regex.
You need to escape some characters.
Try this -
\$\('#[^']*'\)\.val\(\);
Try this regex by escaping special chars:
\$\(.*\).val\(\);
To avoid escaping the special characters, you can use \Q - \E pair to surround the part where you want the regex engine to interpret literally:
\Q$('\E<your-regex>\Q').val();\E
Replace <your-regex> with your regex to match the selector, or whatever it is.

Visual Studio Find and Replace with regex and single/double quotes

How to use VS Find/Replace to replace:
this: $('a[name="lnkFind"]').on('click', function
with this: $(document).on("click", "a[name='lnkFind']", function
I'm not sure which characters need to be escaped - single or double quotes or both? None of the patters I've tried seem to find a match.
You'll need to escape many of these characters.
Find/Replace will complain about the un-escaped ( and ), even the bare ( at the end because it's missing a matching ). Also the square brackets, which are used for character sets, and finally the $.
So this should work as the pattern:
\$\('a\[name="lnkFind"\]'\).on\('click', function
You should look at a list of special characters in Regular Expressions.
$, ., [, ] should all be escaped.
http://www.fon.hum.uva.nl/praat/manual/Regular_expressions_1__Special_characters.html
Except in special cases (such as vim regex), in general you can escape any and all special characters in regex to get their literal form, i.e. escaping a special character that doesn't need to be escaped, won't do any harm.
That said, here's the minimum that needs to be escaped:
\$\('a\[name="lnkFind"]')\.on\('click', function
I don't think you'll need to escape anything in the replacement, because only a $ or \ followed by a number will be interpreted.

Regex for getting domain name from Referer

Am using the following regex to capture different parts of referer url. I want to capture protocol and domain and used it in diff scenarios.
Pattern pr=new Patters("^\w+://|[^\/:]+|[\w\W]*$");
But eclipse is giving me and error
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )..
Am new to regex. Can anyone help me on this?
You're supply a string to the Pattern constructor, so you need to escape the backslashes.
e.g.:
Pattern pr = new Pattern("^\\w+://|[^/:]+|[\\w\\W]*$");
Your regexp is probably not complete - you need to "group" the scheme and domain sections with brackets:
Pattern pr = new Pattern("^(\\w+)://([^/:]+)");
I've ignored everything after the next colon or slash - you said you only wanted the scheme and domain.
Regex uses "\"(i.g., \w, \W, \d, \D) as the starting character to define regex syntax. Java also uses "\" as well. Java also allows "\" to be used by adding an extra "\", so you would end up with "\\" in your code, this will escape the other backslash.
Just in case your solution in not what you expected try using "regexpal.com".
Remember, whenever you expect a single slash("\") in your outcome use a double slash("\\") in your code.