i'm trying to use regex to delete lines which starts with a letter or a number.
for example: if i have a text file with many lines and i want to delete every lines which starts with S and 2.

What operator do i have to use?

i tried this but it prints every line from text.

String[] paras = Pattern.compile[B]("2(.*?)stop")[/B].split(inputText);

Thank you

Try this as your regex: "^[S2]"

"^" is the start-of-line anchor, and [S2] matches either of those characters, so this should match what you're looking for.

(But I could be wrong, regex isn't really my forte)

Edited 5 Years Ago by jon.kiparsky: n/a

I try another example with this code but it's not working as i want.
Here i just tried to delete everything between TR and S in my piece of text

import java.util.regex.*;
import java.io.*;

public class ReadLines
  public static void main(String[] args) throws IOException,InterruptedException
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter file name: ");
    String filename = in.readLine();
    File file = new File(filename);
      System.out.println("File not in text format.");
    else if(!file.exists()){
      System.out.println("File not found.");

    FileInputStream fstream = new FileInputStream(filename);
    DataInputStream ds = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(ds));
    Pattern p;
    Matcher m;
    String strLine;
    String inputText = "";
    while((strLine = br.readLine()) != null) inputText = inputText + strLine + "\n";
    String[] paras = Pattern.compile("(?i)(T.*)[\\s]").split(inputText);
   //String nouveauTest = inputText.replaceAll("(?m)^T.*$","");

    Thread th = new Thread();
    for(int i=0; i<=paras.length-1; i++){

Any help is welcome, thank you

You have a text file. It is split into lines. You are reading it a line at a time. Read a line and if it starts with S and 2 then ignore it. String.startswith() or something like that. Lookup String class.

Ok but i insist to use regex in java.

so i'm trying many ways to find out the solutions to delete lines from text which start with some letters.

i want to Thank you, your method is another solution too.

This article has been dead for over six months. Start a new discussion instead.