0

I have this code:

phraseSources.ToList().ForEach(i => i.JmdictMeaning ?? );

What I need to do, and I'm not it's possible using LINQ, is to remove all occurrences of a string looking like this:

[see=????????]

Note the ??? is meant to indicate there can be any amount of characters, except "]".

That appear inside of JmDictMeaning. Note there might be one or more of these but they will always start with "[see=" and end with "]"

2
  • 4
    Could you provide some sample data and expect result? Commented Oct 21, 2019 at 14:50
  • 1
    Two things to be aware of: ToList will create a copy of a source string list, and ForEach will not modofy the list in-place. So you're "modifying" a copy of a copy of each string. Commented Oct 21, 2019 at 15:15

3 Answers 3

1

In order to remove all [see=...] patterns you can try Regex.Replace:

  using System.Text.RegularExpressions;

  ...

  // Add ", RegexOptions.IgnoreCase" if required (if "See", "SEE" should be matched)
  Regex regex = new Regex(@"\[see=[^\]]*\]");

  // "abc[see=456]789" -> "abc789"
  var result = regex.Replace(source, "");

In your case:

  Regex regex = new Regex(@"\[see=[^\]]*\]"); 

  var list = phraseSources.ToList();

  list.ForEach(item => item.JmdictMeaning = regex.Replace(item.JmdictMeaning, ""));

Same idea if you want to filter out items with such strings:

  var result = phraseSources
    .Where(item => !regex.IsMatch(item.JmdictMeaning))
    .ToList();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Dmitry, I will leave mark your answer as correct in a while as leaving the question open will allow more people to see and upvote your very good answer.
@Alan2: It would be more polite for others if you mark the answer correct as soon as you're sure it's correct, so people can focus their energies helping other people with questions.
0
phraseSources.ToList().RemoveAll(i => i == "xyz");

Yours I imagine would probably look something like

phraseSources.ToList().RemoveAll(i => i.StartsWith("[see=") && i.EndsWith("]"));

Here's an example dotnetfiddle showing it in action

1 Comment

RemoveAll doesn't return the list, and .ToList() creates a copy, so this code will create a copy, modify that copy, and then throw that copy away.
0

You can remove like this:

phraseSources.Select(ph => {ph.JmdictMeaning.Replace("[see=????????]", ""; return ph;})
    .ToList();

Let me show an example:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

and query should look like this:

IList<Person> persons = new List<Person>()
{
    new Person(){FirstName = "one1[see=????????]", LastName = "LastName1" },
    new Person(){FirstName = "two1[see=????????]", LastName = "LastName1" },
    new Person(){FirstName = "three1", LastName = "LastName1" },
    new Person(){FirstName = "one[see=????????]", LastName = "LastName1" },
    new Person(){FirstName = "two", LastName = "LastName1" },
};

persons = persons.Select(p => { p.FirstName = p.FirstName.Replace("[see=????????]", ""); 
    return p; })
    .ToList();

1 Comment

I'm sorry, the ?? is just meant to indicate there can be any string there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.