Java - 正则表达式从第一个数字获取值

时间:2017-04-18 11:24:52

标签: java regex

我有以下要求 我有一个字符串 import org.apache.spark._ import org.apache.spark.rdd.NewHadoopRDD import org.apache.hadoop.hbase.{HBaseConfiguration, HTableDescriptor} import org.apache.hadoop.hbase.client.HBaseAdmin import org.apache.hadoop.hbase.mapreduce.TableInputFormat import org.apache.hadoop.fs.Path; import org.apache.hadoop.hbase.HColumnDescriptor import org.apache.hadoop.hbase.util.Bytes import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.mapred.TableOutputFormat import org.apache.hadoop.mapred.JobConf import org.apache.hadoop.hbase.io.ImmutableBytesWritable import org.apache.hadoop.mapreduce.Job import org.apache.hadoop.mapreduce.lib.input.FileInputFormat import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat import org.apache.hadoop.hbase.KeyValue import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat import org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles val tableName = "ziedspark" val conf = HBaseConfiguration.create() conf.addResource(new Path("file:///opt/cloudera/parcels/CDH-5.4.7-1.cdh5.4.7.p0.3/etc/hbase/conf.dist/hbase-site.xml")) conf.set(TableInputFormat.INPUT_TABLE, tableName) val admin = new HBaseAdmin(conf) if(!admin.isTableAvailable(tableName)) { print("Creating GHbase Table Creating GHbase Table Creating GHbase Table Creating GHbase Table ") val tableDesc = new HTableDescriptor(tableName) tableDesc.addFamily(new HColumnDescriptor("z1".getBytes())) tableDesc.addFamily(new HColumnDescriptor("z2".getBytes())) admin.createTable(tableDesc) }else{ print("Table already exists!!") } val myTable = new HTable(conf, tableName) for (i <- 414540 to 414545) { var p = new Put(Bytes.toBytes(""+i)) p.add("z1".getBytes(), "name".getBytes(), Bytes.toBytes(""+(i*5))) p.add("z1".getBytes(), "age".getBytes(), Bytes.toBytes("2016-07-01")) p.add("z2".getBytes(), "job".getBytes(), Bytes.toBytes(""+i)) p.add("z2".getBytes(), "salary".getBytes(), Bytes.toBytes(""+i)) myTable.put(p) } myTable.flushCommits() val hBaseRDD = sc.newAPIHadoopRDD(conf, classOf[TableInputFormat], classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable], classOf[org.apache.hadoop.hbase.client.Result]) //error here after creating the table count is not working val count = hBaseRDD.count() print("HBase RDD count:" + count) System.exit(0) ,&#34; "BL req 12345/67890"

我需要BL/1234/67890""12345/67890" 从第一个数字中提取值

有人可以使用java分享这个场景的正则表达式吗?

3 个答案:

答案 0 :(得分:4)

如果在/

之前和之后至少显示一位数字,这将有效
[0-9]+[\/][0-9]+

try it here

答案 1 :(得分:2)

您可以使用以下正则表达式:

[A-Za-z\\s]|(?<=[A-Za-z\\s])/

以下是一个例子:

String pattern = "[A-Za-z\\s]|(?<=[A-Za-z\\s])/";
String s = "BL req 12345/67890";
String s1 = "BL/1234/67890";
System.out.println(s.replaceAll(pattern, ""));
System.out.println(s1.replaceAll(pattern, ""));

答案 2 :(得分:1)

有点乱,但值得尝试并且直截了当,在这里:

public class test {
    private test(){
String firstresult  = "BL req 12345/67890";
String output1 = (firstresult.split("q",2)[1]);

String secondresult = "BL/1234/67890";
String output2 =  (secondresult.split("/",2)[1]);

System.out.println(output1);
System.out.println(output2);
    }
public static void main(String args[]) {
    new test();
}
}

测试。