我有以下字符串:
customer.Id as CustomerID, case when customer.Name...
我想从我的字符串中删除as CustomerID
,即所有以as开头且以,
结尾的句子。我怎样才能做到这一点。我和Linq一起去了,但遗憾的是看起来似乎不起作用:
select(c => c.TakeWhile(ch => ch!= 'As').SkipWhile(c=>c != ','))
正如你在TakeWhile
中看到的,我可以使用char而不是单词。如何使用SubString
或Linq
或Regex?
答案 0 :(得分:4)
您可以使用
var s = "customer.Id as CustomerID, case when customer.Name...";
var res = Regex.Replace(s, @"\s+as\s+[^,]+","");
Console.WriteLine(res);
// => customer.Id, case when customer.Name...
请参阅C# demo
正则表达式(see demo)是
\s+as\s+[^,]+
匹配:
\s+
- 1+空格as
- 单词as
\s+
- 1+空格[^,]+
- 除,
以外的1个字符答案 1 :(得分:1)
虽然这个实现远非完美或最简洁,但它至少可以作为一个示例,如何在不使用Regex的情况下完成此操作,而是使用一些带有Linq的标准String API。
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import static java.lang.System.out;
import java.util.ArrayList;
import javax.swing.JFrame;
public class RobinHoodApp{
public static void main(String[] args) throws FileNotFoundException, IOException {
RobinHood app = new RobinHood();
app.readFile();
app.arrayList();
app.wordCount();
app.countMenAtArms();
app.writeToFile();
}
}
class RobinHood extends JFrame
{
private static final ArrayList<String>words = new ArrayList<>();
private static Scanner book;
private static int count;
private static int wordCount;
public RobinHood()
{
try {
// scrubber();
//Prints All Words 1 by 1: Works!
book = new Scanner(new File("RobinHood.txt") );
book.useDelimiter("\r\n");
} catch (FileNotFoundException ex)
{
out.println("Where's your text fam?");
}
}
void readFile()
{
while(book.hasNext())
{
String text = book.next();
out.println(text);
}
void arrayList() throws FileNotFoundException();
{
Scanner add = new Scanner(new File("RobinHood.txt"));
while(add.hasNext())
{
words.add(add.next());
}
}
void output()
{
out.println(words);
}
void countMenAtArms()
{
//Shows 23 times
String find = "men-at-arms";
count = 0;
int x;
String text;
for(x=0; x< wordCount; x++ )
{
text = words.get(x);
text = text.replaceAll("\n", "");
text = text.replaceAll("\n", "");
if (text.equals(find))
{
count++;
}
}
out.println("The amount of time 'men-at-arms' appears in the book is: " + count);
}
// void scrubber()
// {
//
// }
//
//
void wordCount()
{
{
wordCount=words.size();
out.println("There are "+wordCount+" words in Robin Hood.");
}
}
public void writeToFile()
{
File file;
file = new File("Dominique.dat");
try (FileOutputStream data = new FileOutputStream(file)) {
if ( !file.exists() )
{
file.createNewFile();
}
String wordCountSentence = "There are "+ wordCount +" words in Robin Hood. \n";
String countTheMen = "The amount of time 'men-at-arms' appears in the book is: " + count;
byte[] strToBytes = wordCountSentence.getBytes();
byte[] menToBytes = countTheMen.getBytes();
data.write(strToBytes);
data.write(menToBytes);
data.flush();
data.close();
}
catch (IOException ioe)
{
System.out.println("Error");
}
}
}
}
答案 2 :(得分:1)
string sql = "customer.Id as CustomerID, case when customer.Name...";
string sql2 = Regex.Replace(sql, @"\Was.+?(?=,)", "");