This question already has an answer here:
Split Method with single quotes
(1 answer)
Closed 1 year ago.
I need to split a string with the character ' using string.Split()
However, the ' character is used in the string.Split()
For example:
string.Split(''')
but that gives a syntax error.
I have tried using the # symbol to represent a literal string, but that does not work either.
string.Split(#''')
Add escape sequences before single quote .Split('\''), like
var str = "Hell'o'Wo'rl'd";
var output = str.Split('\''); //["Hell", "o", "Wo", "rl", "d"]
.NET FIDDLE
Related
This question already has answers here:
How do I write a backslash (\) in a string?
(6 answers)
Why \b does not match word using .net regex
(2 answers)
Closed 3 years ago.
I have regex like this:
(?i)^(?!.*\bWITH\b).*\(\s*.*\s*\b(INDEX|FASTFIRSTROW|HOLDLOCK|SERIALIZABLE|REPEATABLEREAD|READCOMMITTED|READUNCOMMITTED|ROWLOCK|PAGLOCK|TABLOCK|TABLOCKX|NOLOCK|UPDLOCK|XLOCK|READPAST)\b\s*.*\s*\)
It return true in http://regexstorm.net.
But when i run in C#, it always return false.
String input to text:
INNER JOIN t_hat_meisaimidasi AS MM (READCOMMITTED, NOLOCK) WHERE ( AND hat_kanri_no = ?
Can someone explain me why?
Returns true for me; probably you didn't use #"...", so the escape tokens (\b etc) aren't what you think they are:
Console.WriteLine(Regex.IsMatch(
#"INNER JOIN t_hat_meisaimidasi AS MM (READCOMMITTED, NOLOCK) WHERE ( AND hat_kanri_no = ?",
#"(?i)^(?!.*\bWITH\b).*\(\s*.*\s*\b(INDEX|FASTFIRSTROW|HOLDLOCK|SERIALIZABLE|REPEATABLEREAD|READCOMMITTED|READUNCOMMITTED|ROWLOCK|PAGLOCK|TABLOCK|TABLOCKX|NOLOCK|UPDLOCK|XLOCK|READPAST)\b\s*.*\s*\)"));
Note: "\b" is a string of length 1 that contains a backspace character; #"\b" is a string of length 2 that contains a slash and a b. When dealing with regex, you almost always want to use a verbatim string literal (#"...").
To make it even better: Visual Studio will use colorization to tell you when you're getting it right:
This question already has answers here:
Regular Expression Except this Characters
(4 answers)
C# Regular Expressions, string between single quotes
(4 answers)
Closed 5 years ago.
How do I match anything between single quotes? I need to match all attribute = 'some value' statements within a WHERE clause of queries. I tried:
= '(.+)'
But that doesn't work: somehow messes up all single quotes and matches.
If anyone could help me out it'd be much appreciated!
Try:
= '([^']*)'
Meaning you want anything/everything from = ' that isn't a single quote up to a single quote.
Python example:
import re
text = "attribute = 'some value'"
match = re.search("= '([^']*)'", text)
print(match.group(1))
To read more about that, it is called a negated character class: https://www.regular-expressions.info/charclass.html
This question already has answers here:
Remove text in-between delimiters in a string (using a regex?)
(5 answers)
Closed 6 years ago.
I am trying to remove characters starting from (and including) rgm up to (and including) ;1..
Example input string:
Sum ({rgmdaerudsb;1.Total_Value}, {rgmdaerub;1.Major_Value})
Code:
string strEx = "Sum ({rgmdaerudsb;1.Total_Value}, {rgmdaerub;1.Major_Value})";
strEx = strEx.Substring(0, strEx.LastIndexOf("rgm")) +
strEx.Substring(strEx.LastIndexOf(";1.") + 3);
Result:
Sum ({rgmdaerub;1.Total_Value}, {.Major_Value})
Expected result:
Sum ({Total_Value}, {Major_Value})
Note: only rgm and ;1. will remain static and characters between them will vary.
I would recommend to use Regex for this purpose. Try this:
string input = "Sum ({rgmdaerudsb;1.Total_Value}, {rgmdaerub;1.Major_Value})";
string result = Regex.Replace(input, #"rgm.*?;1\.", "");
Explanation:
The second parameter of Regex.Replace takes the pattern that consists of the following:
rgm (your starting string)
. (dot - meaning any character)
*? (the preceding symbol can occure zero or more times, but stops at the first possible match (shortest))
;1. (your ending string - the dot needed to be escaped, otherwise it would mean any character)
You need to use RegEx, with an expression like "rgm(.);1\.". That's just off the top of my head, you will have to verify the exact regular expression that matches your pattern. Then, use RegEx.Replace() with it.
This question already has answers here:
Regular Expression to find a string included between two characters while EXCLUDING the delimiters
(13 answers)
Closed 7 years ago.
Him I would like to split string by two characters.
For example I have string like this one:
"xx-aa-[aa]-22-[bb]".
I want to retrieve string array of [aa] and [bb]. All characters between [ ].
First I can split by '-', so I'll have string array
var tmp = myString.Split('-');
But now how can I retrieve only strings between [] ?
You can use following regex:
\[(.+?)\]
Use global flag to match all the groups.
Demo
Explanation
(): Capturing Group
\[: Matches [ literal. Need to escape using \
.+?: Non-greedy match any number of any characters
\]: Matches ] literal. Need to escape using \
Visualization
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
String.Format exception when format string contains “{”
Does following possible using C# String.Format?
Required output "products/item/{itemId}"
I've tried escaping braces but this does not work:
const string itemIdPattern = "itemId";
string result = String.Format("products/item/{{0}}", itemIdPattern);
Preferably something more nice than
string result = String.Format("products/item/{0}{1}{2}",
"{",
itemIdPattern,
"}");
You'll need 3 braces per side for that -- 2 for the braces and 1 for the replacement.
string result = String.Format("products/item/{{{0}}}", itemIdPattern);