I have a long string. From this i need to find a part of string and also cut them after a certain characters. For better understanding i have added code example also with output i want from this string.
string myStr = "/NETGEAR-N300-WiFi-Range-Extender/dp/B00L0YLRUW/ref=sr_1_1?keywords=0606449104899&qid=1548142454&sr=8-1";
So basically i need to find the character by /dp/ and grab after this until i find next /. This is the main pattern i want to use to achieve my output
//output i want like this- B00L0YLRUW
From what you described, no need to get fancy, you can use old-school split.
var result = myStr.Split('/')[3]
However, if you string format is not so clear, regex is your friend
Related
if i use regex to split a string, and my pattern defines the start and end characters for the split. what will happen to partial data on the end of the string that does not match the pattern? will it still be included in the array like a normal split would? or would it be bypassed since i am telling it what the start and end characters should be?
This is a rare occurrence but will happen. they expect this to be accounted for. i need to know if the partial data will be included or ignored. nothing i have read so far talks about a pattern for the entire string, rather they just talk about the delimiter. if the data that does match is not included, is there a way to get it? or do i need to do custom parsing?
thanks in advance
string pattern = byteStart + #"[\s.\S]*?" + byteEnd;
string[] str = Regex.Split(e.Text, #pattern);
I have the following string:
21>Please be specific. What do you mean by that?21>Hello are you there623>Simon?
I want to split it into:
21>Please be specific. What do you mean by that?
21>Hello are you there
623>Simon?
Basically the splitter is the numeric value (21 and 623 in this case) followed by the >.
My implementation is that I find the > char, then walk back until I find a non-numeric value.
So basically using sub-string and the like. But it's ugly and I am certain there is a better Regex implementation, but I don't know enough about it.
So can Regex be used here?
You can achieve that with look ahead and look behind, so that your match is the zero length area between what you want to split.
string s = "21>Please be specific. What do you mean by that?21>Hello are you there623>Simon?";
Regex reg = new Regex(#"(?<=\D)(?=\d+>)");
var r = reg.Split(s);
foreach(var i in r)
Console.WriteLine(i);
Will output
21>Please be specific. What do you mean by that?
21>Hello are you there
623>Simon?
Try with following regex. It matches the zero width between something and number>
Regex: (?<=\D)(?=\d+>) replaced with \n for demo.
Explanation:
(?<=\D) looks behind to see if it's not a number.
(?=\d+>) looks ahead to see if it's a number>.
And matched the zer0-width between them.
Regex101 Demo
Try: [0-9]+>
Explanation:
[0-9]+ At least 1 digit
> followed by >
It might make sense to replace the matches with \n$0, which will move them to individual lines.
i have the following regex:
private string tokenRegEx = #"\[%RC:(\w+)%\].*?";
which is when i pass in the string below it finds it:
[%RC:TEST%]
However the following returns false
[%RC:TEST ITEM%]
how can i modify the regex to allow for spaces as well as whole words?
You need to change the \w pattern (which matches alphanum plus underscore only) to something more liberal. For example this would also allow whitespace:
private string tokenRegEx = #"\[%RC:((\w|\s)+)%\].*?";
Of course the "correct" solution would need to take into account exactly what you consider acceptable input, which is kind of open to discussion at this point.
Try this:
#"\[%RC:(\w|\s)+%\].*?";
This would do it, you have to match a space too. You use a group () but using a set [] is less expensive
private string tokenRegEx = #"\[%RC:([ \w]+)%\].*?";
This has probably been answered somewhere before but since there are millions of unrelated posts about string formatting.
Take the following string:
24:Something(true;false;true)[0,1,0]
I want to be able to do two things in this case. I need to check whether or not all the following conditions are true:
There is only one : Achieved using Split() which I needed to use anyway to separate the two parts.
The integer before the : is a 1-3 digit int Simple int.parse logic
The () exists, and that the "Something", in this case any string less than 10 characters, is there
The [] exists and has at least 1 integer in it. Also, make sure the elements in the [] are integers separated by ,
How can I best do this?
EDIT: I have crossed out what I've achieved so far.
A regular expression is the quickest way. Depending on the complexity it may also be the most computationally expensive.
This seems to do what you need (I'm not that good so there might be better ways to do this):
^\d{1,3}:\w{1,9}\((true|false)(;true|;false)*\)\[\d(,[\d])*\]$
Explanation
\d{1,3}
1 to 3 digits
:
followed by a colon
\w{1,9}
followed by a 1-9 character alpha-numeric string,
\((true|false)(;true|;false)*\)
followed by parenthesis containing "true" or "false" followed by any number of ";true" or ";false",
\[\d(,[\d])*\]
followed by another set of parenthesis containing a digit, followed by any number of comma+digit.
The ^ and $ at the beginning and end of the string indicate the start and end of the string which is important since we're trying to verify the entire string matches the format.
Code Sample
var input = "24:Something(true;false;true)[0,1,0]";
var regex = new System.Text.RegularExpressions.Regex(#"^\d{1,3}:.{1,9}\(.*\)\[\d(,[\d])*\]$");
bool isFormattedCorrectly = regex.IsMatch(input);
Credit # Ian Nelson
This is one of those cases where your only sensible option is to use a Regular Expression.
My hasty attempt is something like:
var input = "24:Something(true;false;true)[0,1,0]";
var regex = new System.Text.RegularExpressions.Regex(#"^\d{1,3}:.{1,9}\(.*\)\[\d(,[\d])*\]$");
System.Diagnostics.Debug.Assert(regex.IsMatch(input));
This online RegEx tester should help refine the expression.
I think, the best way is to use regular expressions like this:
string s = "24:Something(true;false;true)[0,1,0]";
Regex pattern = new Regex(#"^\d{1,3}:[a-zA-z]{1,10}\((true|false)(;true|;false)*\)\[\d(,\d)*\]$");
if (pattern.IsMatch(s))
{
// s is valid
}
If you want anything inside (), you can use following regex:
#"^\d{1,3}:[a-zA-z]{1,10}\([^:\(]*\)\[\d(,\d)*\]$"
I'd thought i do a regex replace
Regex r = new Regex("[0-9]");
return r.Replace(sz, "#");
on a file named aa514a3a.4s5 . It works exactly as i expect. It replaces all the numbers including the numbers in the ext. How do i make it NOT replace the numbers in the ext. I tried numerous regex strings but i am beginning to think that its a all or nothing pattern so i cant do this? do i need to separate the ext from the string or can i use regex?
This one does it for me:
(?<!\.[0-9a-z]*)[0-9]
This does a negative lookbehind (the string must not occur before the matched string) on a period, followed by zero or more alphanumeric characters. This ensures only numbers are matched that are not in your extension.
Obviously, the [0-9a-z] must be replaced by which characters you expect in your extension.
I don't think you can do that with a single regular expression.
Probably best to split the original string into base and extension; do the replace on the base; then join them back up.
Yes, I thing you'd be better off separating the extension.
If you are sure there is always a 3-character extension at the end of your string, the easiest, most readable/maintainable solution would be to only perform the replace on
yourString.Substring(0,YourString.Length-4)
..and then append
yourString.Substring(YourString.Length-4, 4)
Why not run the regex on the substring?
String filename = "aa514a3a.4s5";
String nameonly = filename.Substring(0,filename.Length-4);