我正在尝试调用方法。方法应该是找到存储在字符串中的数字。这个数字是我的后端数据库目的的ID号。现在,每当调用此方法时,它应该删除包括" - "在该号码的开头添加,并且还应删除"消息:"。
以下是我正在尝试运行的代码。
public void RegexMatch()
{
string pat = @"-{1,}[0-9]*$";
Regex r = new Regex(pat, RegexOptions.IgnoreCase);
}
string void Main(strings[] args)
{
String myLine = "Message: Data have been confirmed -564673154463";
if (myLine.StartWith("Message: "))
{
myLine = myLine.Replace("Message: ", "");
myLine = RegexMatch()// replacing numbers with blank
}
}
原始代码:
String myLine = "Message: You Data is confirmed -564673154463";
if (myLine.StartsWith("Message"))
{
string pat = @"-{1,}[0-9]*$";
Regex r = new Regex(pat, RegexOptions.IgnoreCase);
Match m = r.Match(myLine);
myLine = myLine.Replace("Message: ", "");
myLine = r.Replace(myLine, "");
string output = myLine.TrimStart();
Console.WriteLine(output);
}
问题 我在RegexMatch()中缺少哪些内容可以帮助我调用该方法?
任何人都可以帮我吗?如果有人不理解任何部分,请在此发布问题以获得澄清。
答案 0 :(得分:1)
如果您想要替换数字,那么只需替换:
String source = "Message: Data have been confirmed -564673154463";
String result = Regex.Replace(source, "-?[0-9]+", "");
请注意,我已经更改了模式:{,1}
只是?
而*
中的[0-9]*
表示单-
是*
一个号,所以我已将+
更改为 public void mostraTela(char tabuleiro[][]){
System.out.println("\n Lines");
for(int lines = 8 ; lines > 0 ; linnes--){
System.out.print(" "+linha + " ");
for(int coluna = 1 ; coluna < 9 ; coluna++){
System.out.print(" "+ tabuleiro[lines][coluna]);
}
System.out.println();
}
System.out.println("\n 1 2 3 4 5 6 7 8");
System.out.println(" Colunas");
。
答案 1 :(得分:1)
如果您希望在整个应用程序中使用最简单的方法,可以创建这样的扩展方法:
package com.name.tictactoe;
import java.util.Scanner;
public class Game {
static boolean turn, win;
static GameBoard[] gameboard;
static Scanner kb = new Scanner(System.in);
static int input;
public static void main(String[] args){
gameboard = new GameBoard[9];
reset();
displayStates();
askTurn();
displayStates();
askTurn();
}
public static void askTurn() {
System.out.println();
System.out.println();
System.out.println("Where do you want to go? Use the numbers shown, where the first segment is the top and the last is the bottom - left to right.");
input = kb.nextInt();
if(input > 8){
System.out.println("Input out of bounds. Game over by default.");
try {
Thread.sleep(1000000000);} catch (InterruptedException e) {e.printStackTrace();}
}
gameboard[input].setState(2);
}
public static void reset(){
for(int i = 0; i < 9; i++){
gameboard[i].setState(0);
}
}
public static void displayStates(){
for(int i = 0; i < 9; i++){
System.out.print(gameboard[i].getState() + " ");
if(i ==2 || i ==5){
System.out.print(" II ");
}
}
System.out.println();
for(int i = 0; i < 9; i++){
System.out.print(i + " ");
if(i ==2 || i ==5){
System.out.print(" II ");
}
}
System.out.println();
}
然后你就像这样称呼它:
public static class ExtensionMethods
{
public static string RemoveNumbers(this string source)
{
return Regex.Replace(source, "-?[0-9]+", "").Trim();
}
}
我还调用String myLine = "Message: Data have been confirmed -564673154463";
if (myLine.StartsWith("Message: "))
{
myLine = myLine.Replace("Message: ", "");
myLine = myLine.RemoveNumbers();
}
Console.WriteLine(myLine);
来删除任何可能的剩余空间,例如之前的数据&#34;数据&#34}。
您可能想要创建一个执行整个逻辑的方法,而不仅仅是Trim()
部分,如果这是您在应用程序中重复使用的内容。例如:
Regex
请注意它是public static class ExtensionMethods
{
public static string GetMessage(this string source)
{
if (source.StartsWith("Message: "))
{
source = source.Replace("Message: ", "");
source = Regex.Replace(source, "-?[0-9]+", "").Trim();
}
return source;
}
}
class Program
{
static void Main(string[] args)
{
String myLine = "Message: Data have been confirmed -564673154463";
Console.WriteLine(myLine.GetMessage());
Console.ReadKey();
}
}
,而不是static void Main
。