I Would like to write a custom regular expression where the format is like XYZ-ABC-001
where
XYZ is fixed,
ABC is variable (it can be any three characters between A-Z )
001 is variable (it can be any number between 0-9)
This will match:
XYZ-[a-zA-Z]{3}-\d+
DIY kit: http://txt2re.com/
Example: http://txt2re.com/index-csharp.php3?s=XYZ-ABC-001&4&22&23&24&-17&-18&14&15&16
Results in the C# output:
string txt="XYZ-ABC-001";
string re1="((?:[a-z][a-z]+))"; // Word 1
string re2="(-)"; // Any Single Character 1
string re3="(.)"; // Any Single Character 2
string re4="(.)"; // Any Single Character 3
string re5="(.)"; // Any Single Character 4
string re6="(-)"; // Any Single Character 5
string re7="(\\d)"; // Any Single Digit 1
string re8="(\\d)"; // Any Single Digit 2
string re9="(\\d)"; // Any Single Digit 3
Regex r = new Regex(re1+re2+re3+re4+re5+re6+re7+re8+re9,RegexOptions.IgnoreCase|RegexOptions.Singleline);
Match m = r.Match(txt);
if (m.Success)
{
String word1=m.Groups[1].ToString();
String c1=m.Groups[2].ToString();
String c2=m.Groups[3].ToString();
String c3=m.Groups[4].ToString();
String c4=m.Groups[5].ToString();
String c5=m.Groups[6].ToString();
String d1=m.Groups[7].ToString();
String d2=m.Groups[8].ToString();
String d3=m.Groups[9].ToString();
Console.Write("("+word1.ToString()+")"+"("+c1.ToString()+")"+"("+c2.ToString()+")"+"("+c3.ToString()+")"+"("+c4.ToString()+")"+"("+c5.ToString()+")"+"("+d1.ToString()+")"+"("+d2.ToString()+")"+"("+d3.ToString()+")"+"\n");
}
Then you can modify it slightly to adjust to your needs, for example replacing (.) with ([A-Z]).
Related
I have a dataframe as follows:
A B
mediafile 1
filemedia 1
media time 1
time media 1
How do I remove the word "media" only if it is the last string in the column. Final Output:
A B
mediafile 1
file 1
media time 1
time 1
Thanks!
In regex, $ means "end of the string", so media$ will match media only if it is immediately followed by the end of the string.
Use gsub for find/replace:
your_data$A = gsub(pattern = "media$", replacement = "", x = your_data$A)
R uses regex the same as any other language, so in the future I'd recommend searching SO for something like "[regex] at end of string", which turned up this question, from which you probably could have generalized.
I have a string. I want to split the string on uneven white spaces. if the white spaces are more or equal to 2 spaces in length than I want them to be in seperate array item, but if there is only one space then I want them to be in same array item so for e.g.
I have this string
1234 This is a Test PASS 1255432 12/21/2016 07:14:11
so when I split the above string, it should be like this
arr(0) = 1234
arr(1) = This is a test ' because it has only one space in between, it there are more or equal to two spaces than I want it to be a seperate item in an array
arr(2) = Pass
arr(3) = 1255432
arr(4) = 12/21/2016
arr(5) = 07:14:1
same thing with below string:
0001 This is a Marketing text_for the students TEST2 468899 12/23/2016 06:23:16
When I split the above string, it should like this:
arr(0)=0001
arr(1) = This is a Marketing text_for the students
arr(2) = Test2
arr(3)=468899
arr(4)=12/23/2016
arr(5) = 06:23:16
Is there any regular expression that can help me to split the string based on spaces, but put together the words if the space is more or equal to 2.
Any help will be greatly appreciated.
This can be done with this regex (\s{0,1}\S+)+ like so:
string text = "0001 This is a Marketing text_for the students TEST2 468899 12/23/2016 06:23:16";
Regex regex = new Regex(#"(\s{0,1}\S+)+");
var matches = regex.Matches("0001 This is a Marketing text_for the students TEST2 468899 12/23/2016 06:23:16").Cast<Match>()
.Select(m => m.Value)
.ToArray();
Console.WriteLine(String.Join( ",", matches));
This is a working java Script snippet of the same thing.
var value = "0001 This is a Marketing text_for the students TEST2 468899 12/23/2016 06:23:16";
var matches = value.match(
new RegExp("(\\s{0,1}\\S+)+", "gi")
);
console.log(matches)
This regex (\s{0,1}\S+)+ works by matching 0 or 1 spaces at the begging of each match with \s{0,1} and then any number of things that aren't a space with \S+ it then matches this whole thing any number of times by containing it in parentheses and using the + operator (...)+, this allows for the string together of single spaces characters.
I have a string like "123456 abcdefgh ijkl mno 78903 "
Now, I have to split into three parts i.e
string1- "123456"
string2 - "abcdefgh ijkl mno"
string3 - "78903"
So your common splitter is a space, which essencially lets you do this:
public void Example()
{
var content = "123456 abcdefgh ijkl mno 78903";
var parts = content.Split(' '); // split on spaces
var string1 = parts.First();
var string3 = parts.Last();
var theRest = content
.Replace(parts.First(), "")
.Replace(parts.Last(), "");
}
What I basically did over here is to split the original string on space, so I have an array of parts, then I get the first and the last part, and finally, I remove those from the content to provide what is left.
I'm storing some files in database which has filename like 1839341255115211butterflies.jpg.I need to show this filename to the user as butterflies.jpg.I need to remove the first 16 digit and then show the filename.Added to it I also have few filenames which don't have this 16digit addition prior to the filename.Now my question is how do I identify if this string has 16digit numeric value prior to the filename, based on it remove the 1st 16digit and display just the filename. I'm aware of how to remove the first 16digit and retrive the filename but need help on how to identify a string that has 16digit.
Any suggestion is much appreciated.
A regular expression looks like a good fit here:
^[0-9]{16}
The above will match on strings that start with 16 digits (0 to 9).
Usage:
if(Regex.Match(fileName, #"^[0-9]{16}").Success)
{
fileName = fileName.Remove(0, 16);
}
string.Remove will work quite nicely:
var str = "1839341255115211butterflies.jpg";
str = str.Remove(0, 16);
Console.WriteLine(str);
With Linq:
remove all digits at the beginning until 16 digits:
string file = "1839341255115211butterflies.jpg";
string extension = Path.GetExtension(file);
string fileName = Path.GetFileNameWithoutExtension(file);
fileName = new string(fileName.Where((c, i) => i >= 17 || !Char.IsDigit(c)).ToArray());
file = fileName + extension;
Demo
Edit: If you just want to know if the first 16 chars are digits, it's easier and more readable:
bool startsWith16Digits = file.Take(16).All(Char.IsDigit);
I have got a string in the form:
8 AM;10:15 AM;3:30 PM;6:15 PM
which i need to format in the form
8 AM-10:15 AM, 3:30 PM-6:15 PM
How can i achieve this?
You could do string splitting on ;, like this
var parts = s.Split(";".ToCharArray());
Then you could use the parts, like this:
var result = string.Format("{0}-{1}, {2}-{3}", parts);
This requires that the string always contains 4 parts.