Custom function on Regex replace in Java [closed] - c#

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
I need to replace all words in a text by applying a specific replacement method Modify(). I have the following code snippet in C#:
Regex regex = new Regex("[A-Za-z][a-z]*");
regex.Replace(text, x => Modify(x.Value));
The Modify() function is some function that is executed to modify each match, for example it could replace all the characters in a word with the next alphabetical character. For example, if this is the input text:
Magic banana is eating the apple.
This could be the output:
Nbhjd cbobob jt fbujoh uif bqqmf.
The purpose of the Modify() function is irrelevant here. I am wondering about the Java implementation of the MatchEvaluator. The code is fairly simple in C#, but how would this be achieved in Java?

How about something along this lines:
public static void main(String[] args) {
String text = "Magic banana is eating the apple.";
System.out.println("Old text: " + text);
System.out.println("New text: " + getEditedText(text));
}
private static String getEditedText(String text) {
StringBuffer result = new StringBuffer();
Pattern pattern = Pattern.compile("[A-Za-z][a-z]*");
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
matcher.appendReplacement(result, getReplacement(matcher));
}
matcher.appendTail(result);
return result.toString();
}
private static String getReplacement(Matcher matcher) {
String word = matcher.group(0);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
sb.append((char)(c + 1));
}
return sb.toString();
}
This is a slightly edited example of the code that can be found at the bottom of this page.
This is the output you would get:
Old text: Magic banana is eating the apple.
New text: Nbhjd cbobob jt fbujoh uif bqqmf.

Related

Replace new lines symbols in long json string [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 3 years ago.
Improve this question
I have a file, that contains JSON string. Long string. Approx 700k symbols.
I'm trying to deserialize it.
But it contains symbols like \r and \n that should be replaces with comma ,.
I've tried to do it with Regex, but it stuck on it without error.
private static readonly Regex Pattern = new Regex("(\r\n|\r|\n)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
Pattern.Replace(dataString, ",");
Also tried to convert string into StringBuilder and use simple .Replace
private readonly IDictionary<string, string> replacements = new Dictionary<string, string> { { "\r\n", "," }, { "\r", "," }, { "\n", "," } };
foreach (var replacement in this.replacements)
{
dataStringBuilder.Replace(replacement.Key, replacement.Value);
}
The second case was better but till the time when the file becomes larger.
So now I receive stuck for both cases.
Are there any other recommended faster solutions?
You could use a naïve approach of manually copying the string, converting line breaks yourself. This enables you to iterate the underlying character array only once, and avoids costly reallocations of string/StringBuilder objects:
char[] converted = new char[input.Length];
int pos = 0;
bool lastWasCr = false;
foreach(char c in input)
{
if(c == '\r')
{
converted[pos++] = ',';
lastWasCr = true;
}
else
{
if(c == '\n')
{
if(!lastWasCr)
converted[pos++] = ',';
}
else
converted[pos++] = c;
lastWasCr = false;
}
}
string output = new string(converted, 0, pos);
This loop iterates over every character, and detects and replaces line breaks. Note that we have to keep track of recent carriage returns (\r), to avoid double , on Windows line breaks (\r\n).
I compared your two approaches with the code above, using a random 650kb text file, and performing 1000 iterations of each implementation.
Results:
Regex.Replace: 62.3233sec (this does not even include initialization like compiling the regex)
StringBuilder.Replace: 7.0622sec (fixed version as indicated in a comment to your question)
Char-wise loop with if statement: 2.3862sec

Extract the first two words from a string with C# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I guys im trying to workout C# code to extract the first two words from string. below is code im doing.
public static string GetDetailsAsString(string Details)
{
string Items = //how to get first 2 word from string???
if (Items == null || Items.Length == 0)
return string.Empty;
else
return Items;
}
Define "words", if you want to get the first two words that are separated by white-spaces you can use String.Split and Enumerable.Take:
string[] words = Details.Split();
var twoWords = words.Take(2);
If you want them as separate string:
string firstWords = twoWords.First();
string secondWord = twoWords.Last();
If you want the first two words as single string you can use String.Join:
string twoWordsTogether = string.Join(" ", twoWords);
Note that this simple approach will replace new-line/tab characters with empty spaces.
Assuming the words are separated by whitespaces:
var WordsArray=Details.Split();
string Items = WordsArray[0] + ' ' + WordsArray[1];

C# read only part of whole string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have a string which has a following format:
"####/xxxxx"
The text before the "/" is always an integer and I need to read it. How do I get only the integer part of this string (before the "/")?
Thank you for your help.
You can split the string on / and then use int.TryParse on the first element of array to see if it is an integer like:
string str = "1234/xxxxx";
string[] array = str.Split(new []{'/'}, StringSplitOptions.RemoveEmptyEntries);
int number = 0;
if (str.Length == 2 && int.TryParse(array[0], out number))
{
//parsing successful.
}
else
{
//invalid number / string
}
Console.WriteLine(number);
Use IndexOf and Substring:
int indexOfSlash = text.IndexOf('/');
string beforeSlash = null;
int numBeforeSlash = int.MinValue;
if(indexOfSlash >= 0)
{
beforeSlash = text.Substring(0, indexOfSlash);
if(int.TryParse(beforeSlash, out numBeforeSlash))
{
// numBeforeSlash contains the real number
}
}
Another alternative: use a regular expression:
var re = new System.Text.RegularExpression(#"^(\d+)/", RegexOptions.Compiled);
// ideally make re a static member so it only has to be compiled once
var m = re.Match(text);
if (m.IsMatch) {
var beforeSlash = Integer.Parse(re.Groups[0].Value);
}

Removing everything in a string before a specific phrase and after a specific phrase [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
I have a string and I want to delete everything before a phrase, and then delete everything after a different phrase. i.e.,
myString = "words words words FIRSTPHRASE these words I want SECONDPHRASE but not these words"
So the new string would be "these words I want".
Use String.Substring and String.IndexOf, it has also an overload with a start index:
string myString = "words words words FIRSTPHRASE these words I want SECONDPHRASE but not these words";
string result = myString;
int indexOfFirstPhrase = myString.IndexOf("FIRSTPHRASE");
if(indexOfFirstPhrase >= 0)
{
indexOfFirstPhrase += "FIRSTPHRASE".Length;
int indexOfSecondPhrase = myString.IndexOf("SECONDPHRASE", indexOfFirstPhrase);
if (indexOfSecondPhrase >= 0)
result = myString.Substring(indexOfFirstPhrase, indexOfSecondPhrase - indexOfFirstPhrase);
else
result = myString.Substring(indexOfFirstPhrase);
}
Demonstration
Something like this?
string theWordsIWant = Regex.Replace(myString, #"^.*?FIRSTPHRASE\s*(.*?)\s*SECONDPHRASE.*$", "$1");
Demonstration

How to split a formatted string? [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
string text = "try your {{equal|even out|regularize} out|steady|tied|equal}";
Actually I want to split all words within the outer brackets ({}).
For example, if this is the string: "{{equal|even out|regularize} out|steady|tied|equal}", I want the split result array to be this:
{ equal,
even out,
regularize,
out,
steady,
tied,
equal }
string text = "try your {{equal|even out|regularize} out|steady|tied|equal} {champion|finest|top-quality}";
string1 = "try";
string2 = "your";
string3="{{equal|even out|regularize} out|steady|tied|equal}";
string4="{champion|finest|top-quality}";
How to split like this... please help me... Thanks !
try this :
string text = "try your {{equal|even out|regularize} out|steady|tied|equal}";
text = text.Remove(0, text.IndexOf('{'));
var array = text.Split('{', '}', '|');
array.ToList().ForEach(item =>
{
Console.WriteLine(item);
});
output :
equal
even out
regularize
out
steady
tied
equal

Categories