字符串:" GRUNDIG GWN 36630 Waschmaschine"
输出:" GRUNDIG GWN 36630 "
可以使用
$s2 = preg_replace("/([^0-9]*[0-9]*)(.*)/", "$1", $link->plaintext);
但是,如果我有一个带有点内部的字符串,如: 字符串:" WMF 12.7700.6343 Lingo 24-tlg。 Besteck-集" 输出:" WMF 12"
但它应该是 WMF 12.7700.6343
我需要一个脚本来修剪数字块末尾的字符串,而不是第一个点之前的第一个数字。
更新:09.09.2017
抱歉,我不知道如何使用strackoverflow。
GRUNDIG GWN 36.6-30 Waschmaschine
WMF 12.7700.6343 Lingo 24-tlg。 Besteck-Set
GIGASET C 430 A Duo Schnurloses Telefon
应该是:
GRUNDIG GWN 36.6-30 Waschmaschine WMF 12.7700.6343 Lingo 24-tlg。 GIGASET C 430 A Duo
GIGASET C 430 A Duo
答案 0 :(得分:1)
实际上,您只需要搜索 public interface ExecListener {
void onProcess (String line, int i);
}
public static String exec (String input) throws IOException, InterruptedException {
return exec (input, null);
}
public static String exec (String input, ExecListener listener) throws IOException, InterruptedException {
Process process = Runtime.getRuntime ().exec (input);
BufferedReader in = new BufferedReader (new InputStreamReader (process.getInputStream ()));
StringBuilder output = new StringBuilder ();
int i = 0;
String line;
while ((line = in.readLine ()) != null) {
if (listener == null)
output.append (line).append ("\n");
else
listener.onProcess (line, i);
++i;
}
in.close ();
process.waitFor ();
return output.toString ().trim ();
}
public static void execSU (List<String> cmds) throws IOException, InterruptedException {
Process process = Runtime.getRuntime ().exec ("su");
DataOutputStream os = new DataOutputStream (process.getOutputStream ());
for (String cmd : cmds) os.writeBytes (cmd + "\n");
os.writeBytes ("exit\n");
os.flush ();
os.close ();
process.waitFor ();
}
public static void killProcess (String id) throws IOException, InterruptedException {
return killProcess (new String[] {id});
}
public static void killProcess (final String[] ids) throws IOException, InterruptedException {
final ArrayList<String> procs = new ArrayList<> ();
exec ("ps", new ExecListener () {
@Override
public void onProcess (String line, int i) {
if (i > 0) {
String[] data = Arrays.explode ("\\s+", line);
for (String id : ids)
if (id.equals (data[8]))
procs.add (data[1]);
}
}
});
if (procs.size () > 0) {
ArrayList<String> cmds = new ArrayList<> ();
cmds.add ("kill -9 " + Arrays.implode (" ", procs));
execSU (cmds);
}
}
并将其替换为(^.+?([0-9.-]+)).*
。就是这样。
这是一个包含两个字符串的实时示例: https://regex101.com/r/6fLvpk/2