Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
I have a string
Input: Mynameisblablabla.Istayatblabla
Now i need a regular expression in c# where it automatically splits the string into
My name is blablabla. I stay at blabla
in single step.
Posted
Comments
Ambati Dilip 16-Jan-15 23:47pm    
I dont want to use string.split(0,5); i need only regular expression please. and pass them to string[]. string firstPart = string.Substring(0, 4);

1 solution

You can't sutomatically split
Mynameisblablabla.Istayatblabla

into
My name is blablabla. I stay at blabla
with a regex at all: you need a dictionary of English words in order to find out where each oif the individual wordas ends, and regular expressions don't have any facility for that. They are good, but they aren't miracle workers! :laugh:

You will need to do this yourself, but it's not going to be a simple task. Just organising the dictionary efficiently for this task is going to take quite some time to work out...

Sorry, but there is nothing automatic that you can do here.
 
Share this answer
 
Comments
Ambati Dilip 17-Jan-15 3:30am    
My question is not for search dictionary words and splitting. just another example string: simpletask now i want to split simp, letask, just splitting a string at a particular point that's it. the length of string is 10 i need to split 1-4,5-7,8-10 in this way.
OriginalGriff 17-Jan-15 3:50am    
Then the best way to do it is probably to use substring: since your "sections" aren't all teh same size (4, 3, 3 in your example) you'd have to "fix" it in a regex:
(.{4})(.{3})(.{3})
and that wouldn't work with your original example.

What exactly are you trying to do? Because from here, your examples don;t make a lot of sense. Is it a fixed input like a serial number you are trying to extract sections from?
Ambati Dilip 17-Jan-15 4:46am    
Yes, always my string is fixed length. In that it contains upto 25 characters. Now i want to split the string into array. (1-12)-Phone no with code. (13-20)-Userregistered id (21-25) - Secret code of user. ex: 919963452421AC6214BXCCNY0. i need to split 919963452421,AC6214BX and CCNY0. But i am getting 5 array of string. array[0] and array[4] is null. and array[1],array[2],array[3] have my required data.

Example:
string[] match = null;
string test = "919963452421AC6214BXCCNY0";
Regex re = new Regex(@"(.{12})(.{8})(.{5})");
match = re.Split(test);


Thanks for your reply Mr. Griff. it solved my problem but except those null values in first and last that's it.
OriginalGriff 17-Jan-15 5:18am    
A regex is probably a bit overkill for that - I'd probably use string.Substring to extract the three parts as the code would probably be more readable. To do that with a regex means accessing the Groups, and using Linq to convert the values to an array:

string test = "919963452421AC6214BXCCNY0";
Regex re = new Regex(@"(.{12})(.{8})(.{5})");
string[] match = re.Match(test).Groups.Cast<group>().Select(g => g.Value).ToArray();

And that will give you four array elements:

919963452421AC6214BXCCNY0
919963452421
AC6214BX
CCNY0

As the regex returns the whole match as a separate group.
A cleaner, and probably quicker, approach would be just to use the substrings:

string phoneNo = test.Substring(0, 12);
string userId = test.Substring(12, 8);
string secretCode = test.Substring(20, 5);

It's a lot more obvious what you are doing as well! :laugh:
Ambati Dilip 17-Jan-15 5:27am    
Thank you but anyhow i dont want to use string.substring .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900