正则表达式在C#中删除转义字符(特定的字符)

时间:2015-04-28 06:07:42

标签: c# sql-server regex sqlcommand

下面的正则表达式不是我真正需要的:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.scene.control.Dialog;
import javafx.util.Duration;
import javafx.util.Pair;

public class DialogEarthquakeCenter {

public static final int SHAKE_DISTANCE = 10;
public static final double SHAKE_CYCLE = 50;
public static final int SHAKE_DURATION = 500;
public static final int SHAKE_UPDATE = 5;

private Dialog<Pair<String, String>> dialog;
private int x, y;
private long startTime;
private Timer shakeTimer;
private final double TWO_PI = Math.PI * 2.0;
private Timeline timeline;

public DialogEarthquakeCenter(Dialog<Pair<String, String>> parent) {
    dialog = parent;
}

/**
 * Creates and starts the timer
 *
 * @return Scene
 */
public void startTimer() {
    x = (int) dialog.getX();
    y = (int) dialog.getY();
    startTime = System.currentTimeMillis();
    // Set up earth time text update
    timeline = new Timeline(new KeyFrame(Duration.millis(SHAKE_DURATION), ae -> startNudging()));
    //timeline.setCycleCount(javafx.animation.Animation.INDEFINITE);
    timeline.play();
}

public void startNudging() {
    x = (int) dialog.getX();
    y = (int) dialog.getY();
    startTime = System.currentTimeMillis();
    shakeTimer = new Timer(SHAKE_UPDATE, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            shake();
        }
    });

    shakeTimer.start();
}

public void shake() {
    // calculate elapsed time
    long elapsed = System.currentTimeMillis() - startTime;
    //System.out.println("elapsed is " + elapsed);
    // use sin to calculate an x-offset
    double waveOffset = (elapsed % SHAKE_CYCLE) / SHAKE_CYCLE;
    double angle = waveOffset * TWO_PI;
    // offset the x-location by an amount
    // proportional to the sine, up to shake_distance
    int shakenX = (int) ((Math.sin(angle) * SHAKE_DISTANCE) + x);

    Platform.runLater(() -> {
        //dialog.hide();
        dialog.setX(shakenX);
        //System.out.println("set shakenX to " + shakenX);
        dialog.setY(y);
        dialog.show();
    });

    //try {Thread.sleep(20);}
    //catch (InterruptedException ex) {}

    // should we stop timer
    if (elapsed >= SHAKE_DURATION) {
        stopShake();

    }
}

public void stopShake() {
    shakeTimer.stop();
    Platform.runLater(() -> {
        timeline.stop();
        dialog.close();
    });
}
}

我需要从字符串中删除转义字符,因为我正在创建一个带字符串的SQL,当我有这个字符Regex.Replace(value.ToString(), "[^0-9a-zA-Z]+", "") 或此'等时,我的Sql会生成错误,我无法使用:在这种情况下SqlParameter,因为我只有一个字符串中的SQL列表,但我可以删除我不想要的字符。

所以,我只需要删除这些字符:

\r\n

按要求添加了我的代码:

\r \n ' /\

2 个答案:

答案 0 :(得分:4)

您可以将这些字符放入字符类中,并替换为SELECT game.*, genre.*, perspective.*, operational_environment.* FROM game JOIN gamegen ON gamegen.game_id = game.game_id JOIN genre ON gamegen.genre_id = genre.genre_id JOIN gameper ON gameper.game_id = game.game_id JOIN perspective ON gameper.perspective_id = perspective.perspective_id JOIN gameoe ON gameoe.game_id = game.game_id JOIN operational_environment ON gameoe.oe_id = operational_environment.oe_id WHERE game.game_id = 32;

string.Empty

结果:

enter image description here

字符类通常执行得更快,与替代列表一样,有很多反向跟踪阻碍了性能。

答案 1 :(得分:1)

如果我理解正确,你需要这样的东西:

Regex.Replace(value.ToString(), "(\\\\n|\\\\r|'|\\/\\\\)+", "")

请参阅here