Add space between Number and String - c#

I have a string Like this
"My Train Coming on Track 10B on 6A and string test with 11S"
Now i want to Add space between Number like 11 and Char B and so on
i want to like this
"My Train Coming on Track 10 B on 6 A and string test with 11 S"
using C#. is there any logic for that.
thank

With a regex:
var result = Regex.Replace(str, #"(?<=\d)(?=\p{L})", " ");
This replaces the "empty space" between a digit ((?<=\d)) and a letter ((?=\p{L})) with a space.
A different method without the lookarounds would be:
var result = Regex.Replace(str, #"(\d)(\p{L})", "$1 $2");
In this case, it replaces the last digit and the first letter with the pattern $1 $2, inserting a space in the process.

The Above answer is correct But according to requirement we have to use that like :
var result = Regex.Replace("4A", #"(?=\p{L})(?<=\d)", " ");
I hope it will help you.

Related

C# regex replace of numbers and units

Have a number of product names with sizes in.
For example "X 300g", "X 400 g", "X 250 kg", "X 25kg".
Now I would like to replace all instances of "NumberUnit" with "Number Unit", i.e. "300g" to "300 g", "25kg" to "25 kg", etc.
I know I can do that easily with a loop and a string replace, but there are millions of products and I worry it would take to long to do it that way.
Instead, I guess it might be a better idea to do a Regex.Replace() on each product name.
Agree? - and how would you write the regex?
Thanks.
Try this code:
(?<=\d)(?=[a-zA-Z])
The previous regex will capture the location between numbers and units only if they don't have a space in between. You can replace this location with a space, and you will be done.
Demo: https://regex101.com/r/yry7Sl/3
Try this:
string pattern = #"(?<=\d)(?=[a-zA-Z])";
string substitution = #" ";
string input = #"""X 300g"", ""X 400 g"", ""X 250 kg"", ""X 25kg""";
Regex regex = new Regex(pattern);
string result = regex.Replace(input, substitution);

Remove special characters from string with unicode

I found the most popular answer to this question is:
Regex.Replace(value, "[^a-zA-Z0-9]+", " ", RegexOptions.Compiled);
However, if users type in Non-English name when billing, this method will consider these non- are special characters and remove them.
Is there any way we can build for most of users since my website is multi-language.
Make it Unicode aware:
var res = Regex.Replace(value, #"[^\p{L}\p{M}\p{N}]+", " ");
If you plan to keep only regular digits, keep [0-9].
The regex matches one or more symbols other than Unicode letters (\p{L}), diacritics (\p{M}) and digits (\p{N}).
You might consider var res = Regex.Replace(value, #"\W+", " "), but it will keep _ since the underscore is a "word" character.
I found my self that the best way to achieve this and make work with all languages is create a string with all banned characters, look this code:
string input = #"heya's #FFFFF , CUL8R M8 how are you?'"; // This is the input string
string regex = #"[!""#$%&'()*+,\-./:;<=>?#[\\\]^_`{|}~]"; //Banned characters string, add all characters you don´t want to be displayed here.
Match m;
while ((m = Regex.Match(input, regex)) != null)
{
if (m.Success)
input = input.Remove(m.Index, m.Length);
else // if m.Success is false: break, because while loop can be infinite
break;
}
input = input.Replace(" ", " ").Replace(" "," "); //if string has two-three-four spaces together change it to one
MessageBox.Show(input);
Hope it works!
PS: As others posted here, there are other ways. But I personally prefer that one even though it´s way more code. Choose the one you think better fits for your needing.

Regular expression doesn't match like I want

So, I got an regular expression :
(?<=[0-9])(?=[A-Za-z])|(?<=[A-Za-z])(?=[0-9])
That should found all letters and replace it with a blank.
var nomDoc = Regex.Replace(arr[0], "(?<=[0-9])(?=[A-Za-z])|(?<=[A-Za-z])(?=[0-9])", " ");
But when I got for example :
45a, nomDoc become 45 a, while I juste want 45
Did I write this regex wrong? I'm not very good at it, but I was thinking I was good for this one.
The regex must replace all non-numeric characters, following a numeric character or all non-numeric char before numeric.
45a or a45 must give me 45.
Thank you.
What you're doing, is searching for a spot where the string changes from digits to letters or from letters to digits and insert a space there. So yes, 45a becomes 45 a.
If you want to replace all letters with a blank, use
var nomDoc = Regex.Replace(arr[0], "[A-Za-z]", " ");
But I doubt that this is what you want.
If you want to remove all letters, replace with an empty string instead of a space.
If you want to replace all letters following a digit with a space, use
var nomDoc = Regex.Replace(arr[0], "(?<=[0-9])[A-Za-z]+", " ");
Try this:
var str = "1 oo 23ksls 4910fsj2jd43ld fkkd ^&?&;#";
var nomDoc = str.Replace('/([^0-9]|\n)/g', ' ');
This replaces all the non-number characters(letters, whitespaces and characters) with a space.
You could try this :
var nomDoc = Regex.Replace(arr[0], "[^0-9]", "");
If you are using Javascript, here's a fiddle :
var Str = "blablabla22445543__-_-_-_-_-_-èèpzofez5zsqef*f-e+ffnfuf'3";
var nomDoc = Str.replace(/[^0-9]/g, "");
$("#result").html(nomDoc);
http://jsfiddle.net/ZqF6L/
It's not quite clear if you want to replace all non-numeric characters with spaces or just remove then completely.
Depending on that, either
var nomDoc = Regex.Replace(arr[0], "[^0-9]", " ");
or
var nomDoc = Regex.Replace(arr[0], "[^0-9]", "");
should do what you want.
hey if you want to remove all your words then use below format method
var demo= Regex.Replace(arr[0], "(?<=[0-9])[A-Za-z]+", " ");

Is there a way to get a string up until a year value?

Basically I have some filenames where there is a year in the middle. I am only interested in getting any letter or number up until the year value, but only letters and numbers, not commas, dots, underscores, etc. Is it possible? Maybe with Regex?
For instance:
"A-Good-Life-2010-For-Archive"
"Any.Chararacter_Can+Come.Before!2011-RedundantInfo"
"WhatyouseeIsWhatUget.2012-Not"
"400-Gestures.In1.2000-Communication"
where I want:
"AGoodLife"
"AnyChararacterCanComeBefore"
"WhatyouseeIsWhatUget"
"400GesturesIn1"
By numbers I mean any number that doesn't look like a year, i.e. 1 digit, 2 digits, 3 digits, 5 digits, and so on. I only want to recognize 4 digit numbers as years.
You'll have to do this in two parts -- first to remove the symbols you don't want, and second to grab everything up to the year (or vice versa).
To do grab everything up to the year, you can use:
Match match = Regex.Match(movieTitle,#"(.*)(?<!\d)(?:19|20)[0-9]{2}(?!\d)");
// if match.Success, result is in match.Groups[1].value
I've made the year regex so it only matches things in the 1900s or 2000s, to make sure you don't match four-digit numbers as year if they're not a year (e.g. "Ali-Baba-And-the-1234-Thieves.2011").
However, if your movie title involves a year, then this won't really work ("2001:-Space-Odyssey(1968)").
To then replace all the non-characters, you can replace "[^a-zA-Z0-9]" with "". (I've allowed digits because a movie might have legitimate numbers in the title).
UPDATED from comments below:
if you search from the end to find the year you might do better. ie find the latest occuring year-candidate as the year. Hence, I've changed a .*? to .* in the regex so that the title is as greedy as possible and only uses the last year-candidate as the year.
Added a (?!\d) to the end of the year regex and a (?<!\d) to the start so that it doesn't match "My-title-1" instead of "My-title-120012-fdsa" & "2001" in "My-title-120012-fdsa" (I didn't add the boundary \b because the title might be "A-Good-Life2010" which has no boundary around the year).
changed the string to a raw string (#"...") so I don't need to worry about escaping backslashes in the regex because of C# interpreting backslashes.
you can try like this
/\b\d{4}\b/
d{4}\b will match four d's at a word boundary.Depending on the input data you may also want to consider adding another word boundary (\b) at the beginning.
using System.Text.RegularExpressions;
string GoodParts(string input) {
Regex re = new Regex(#"^(.*\D)\d{4}(\D|$)");
var match = re.Match(input);
string result = Regex.Replace(match.Groups[1].Value, "[^0-9a-zA-Z]+", "");
return result;
}
You can use Regex.Split() to make the code ever so terser (and possibly faster due to the simpler regex):
var str = "400-Gestures.In1.2000-Communication";
var re = new Regex(#"(^|\D)\d{4}(\D|$)");
var start = re.Split(str)[0];
// remove nonalphanumerics
var result = new string(start.Where(c=>Char.IsLetterOrDigit(c)).ToArray());
I suppose you want a fancy regular excpression?
Why not a simple for loop?
digitCount = 0;
for i = 0 to strlen(filename)
{
if isdigit(fielname[i])
{
digitCount++;
if digitCount == 4
thePartOfTheFileNameThatYouWant = strcpy(filename, 0, i-4)
}
else digitCount = 0;
}
// Sorry, I don't know C-sharp

RegEx Problem using .NET

I have a little problem on RegEx pattern in c#. Here's the rule below:
input: 1234567
expected output: 123/1234567
Rules:
Get the first three digit in the input. //123
Add /
Append the the original input. //123/1234567
The expected output should looks like this: 123/1234567
here's my regex pattern:
regex rx = new regex(#"((\w{1,3})(\w{1,7}))");
but the output is incorrect. 123/4567
I think this is what you're looking for:
string s = #"1234567";
s = Regex.Replace(s, #"(\w{3})(\w+)", #"$1/$1$2");
Instead of trying to match part of the string, then match the whole string, just match the whole thing in two capture groups and reuse the first one.
It's not clear why you need a RegEx for this. Why not just do:
string x = "1234567";
string result = x.Substring(0, 3) + "/" + x;
Another option is:
string s = Regex.Replace("1234567", #"^\w{3}", "$&/$&"););
That would capture 123 and replace it to 123/123, leaving the tail of 4567.
^\w{3} - Matches the first 3 characters.
$& - replace with the whole match.
You could also do #"^(\w{3})", "$1/$1" if you are more comfortable with it; it is better known.
Use positive look-ahead assertions, as they don't 'consume' characters in the current input stream, while still capturing input into groups:
Regex rx = new Regex(#"(?'group1'?=\w{1,3})(?'group2'?=\w{1,7})");
group1 should be 123, group2 should be 1234567.

Categories

Resources