how to split base64 text into base64 strings c# [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have a base64 text example:
UAAAAAAAAAA=AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=KAAAAAAAAAA=AAAAAAAA8D8AAAAAAAAAQAAAAAAAAAhAAAAAAAAAEEAAAAAAAAAAAA==
I need to split it into sides like this:
UAAAAAAAAAA=
AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
KAAAAAAAAAA=
AAAAAAAA8D8AAAAAAAAAQAAAAAAAAAhAAAAAAAAAEEAAAAAAAAAAAA==
I tried:
string base64Text = "UAAAAAAAAAA=AA....."
Regex regex = new Regex(#"^[a-zA-Z0-9+/]*={0,2}$");
string[] base64Strings = regex.Split(base64Text);

You could split on a position where there is a char A-Z or 0-9 to the left and not a = char or the end of the string to the right:
(?<=[A-Z0-9]=)(?!=|$)
Regex demo
string base64Text = #"UAAAAAAAAAA=AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=KAAAAAAAAAA=AAAAAAAA8D8AAAAAAAAAQAAAAAAAAAhAAAAAAAAAEEAAAAAAAAAAAA==";
Regex regex = new Regex(#"(?<=[A-Z\d]=)(?!=|$)");
string[] base64Strings = regex.Split(base64Text);
foreach (string s in base64Strings)
{
Console.WriteLine(s);
}
Output
UAAAAAAAAAA=
AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
KAAAAAAAAAA=
AAAAAAAA8D8AAAAAAAAAQAAAAAAAAAhAAAAAAAAAEEAAAAAAAAAAAA==

Related

Regexp - Delete the one word before XXX, remove XXX too [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I need to remove a word XXX in a string and also one word before XXX.
How I can do that with C# Regexp?
Do a single regex replacement:
string input = #"Hello World XXX Goodbye XXX Rabbit!";
Regex rgx = new Regex(#"\s*\w+\s+(?:XXX|xxx)"); // or maybe [Xx]{3}
string result = rgx.Replace(input, "", 1);
Console.WriteLine(result);
Hello Goodbye XXX Rabbit!
Demo
This replacement only would target XXX for removal if it be preceded by a word (one character or more). Explore the demo to see how it would behave with various inputs.
We can also make the search pattern case insensitive via this:
Regex rgx = new Regex(#"\s*\w+\s+XXX", RegexOptions.IgnoreCase);
^^^^^ add this
U can use the replace method :
String s = "aaa bbb";
s = s.Replace("a", "")
// The example displays the following output:
// The initial string: 'aaa bbb'
// The final string: 'bbb'
Or use a Regex in replace :
tmp = s.Replace(n, "[^0-9a-zA-Z]+", "");

How can I return the index of the second word in a string that can have multiple white spaces followed by one word followed by multiple white spaces? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
String str =" vol ABCD C XYZ";
I want to return the index of "A"
Note: There are multiple white spaces before all words including the first.
This will get the index of A in your original example (String str =" vol ABCD C XYZ";):
int indexOfABCD = str.IndexOf(str.Trim()[str.Trim().IndexOf(' ')+1]);
If you have something like String str = " vol ABCD C XYZ"; where there are multiple spaces:
string secondword = str.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)[1];
int indexOfAinABCD = str.IndexOf(secondword.First());
If you want to get the indices of all the letters in the second word:
IEnumerable<int> fullindex = Enumerable.Range(indexOfAinABCD, secondword.Length);
EDIT:
This will fail if you have A anywhere else in the first word. You should get an exact match through Regex:
int indexOfAinABCD = Regex.Match(str, string.Format(#"\W{0}\W",secondword)).Index+1;
IEnumerable<int> fullindex = Enumerable.Range(indexOfAinABCD, secondword.Length);
So something like String str = " vABCDol ABCD C XYZ"; won't be an issue

Compare regular expression [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to compare following string by Regular Expression. I have surf lot of time but unable to get it's pattern.
string str = "Full Name: Atif Mahmood"
+ "ID Number: 12345678901"
+ "Mobile Number: +921234567890";
In above string
Full Name:
ID Number:
Mobile Number:
are necessary with sequence and there should be any string after these constants.
var regex = "Full Name:(.*)ID Number:(.*)Mobile Number:(.*)";
var match = Regex.Match(string, regex);
match.Groups[1] will contain the name, [2] will contain the ID number, etc. (Groups[0] is the whole match group, so counting each match starts at 1)
This probably needs some bullet proofing, but you get the idea?
In case you want to check that the string follows the pattern you stated, this expression should do it:
const string expression = "Full Name:.*ID Number:.*Mobile Number:.*";
bool correct = Regex.IsMatch(str, expression);

Characters between two exact characters [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let me show you what I want to do...
For example, I have this as an input
......1.......1.................
and what I want to do is
.......1111111..................
So I want to fill the space between the two ones with ones...
Also this should be able to be done too:
......11.....1..................
........11111...................
So I want just the inside...
Any C# help you can give?
This can be solved much easier without requiring a regular expression: You just want to "invert" the area of the string delimited by the first and last occurrence of a "1".
Here's an example solution:
string input = "..........1............1...";
int start = input.IndexOf('1');
int end = input.LastIndexOf('1');
char[] content = input.ToCharArray();
for (int i = start; i <= end; i++)
{
content[i] = content[i] == '1' ? '.' : '1'; //invert
}
string output = new string(content);
a regex way:
with multiline mode:
pattern: (?>(?<=^\.*)|\G)1(?=1*(\.))|\G(?<!^)\.(?=\.*(1))
replacement: $1$2
example:
string pattern = #"(?>(?<=^\.*)|\G)1(?=1*(\.))|\G(?<!^)\.(?=\.*(1))";
string input = #"......1.......1.................
......11.....1..................
......11111111..................";
string replacement = "$1$2";
Regex rgx = new Regex(pattern, RegexOptions.Multiline);
string result = rgx.Replace(input, replacement);

What does the regular expression search/seplace pattern do? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What does the <matcher>?() regular expression do when used in the context of a search replace?
string input = "z=""(?<matcher>([a-z]{3,15}))"""
string pattern = z="cat"
string replacement = #"<ANIMAL>${matcher}</ANIMAL>";
string formattedOutput = Regex.Replace(input, pattern, replacement);
The formattedOutput will be "cat" after the expression has evaluated.
You have a lot of errors...
Here is the correction:
string pattern = #"z=\""(?<matcher>([a-z]{3,15}))\""";
string input = #"z=""cat""";
string replacement = #"<ANIMAL>${matcher}</ANIMAL>";
string formattedOutput = Regex.Replace(input, pattern, replacement);
Console.WriteLine(formattedOutput);
?<matcher> is just a named group. You can pick any name. For example the following is equivalent:
string pattern = #"z=\""(?<WHATEVER>([a-z]{3,15}))\""";
string input = #"z=""cat""";
string replacement = #"<ANIMAL>${WHATEVER}</ANIMAL>";
string formattedOutput = Regex.Replace(input, pattern, replacement);
Console.WriteLine(formattedOutput);

Categories