Saya ingin menghapus awalan 0 , + atau spasi (\s) sebelum memulai karakter numerik aktual dari nomor telepon. Mungkin tidak sulit tetapi karena saya baru mengenal regix jadi saya mencari bantuan. Saya mencoba membuat sendiri tetapi saya tidak berhasil.

Berikut adalah tautan yang sejenis tetapi juga menambahkan + terkemuka tetapi saya tidak menginginkannya. pangkas nomor telepon dengan regex

Jadi untuk saya mencoba ini tetapi juga menghapus 0 bagian dalam

(^\s*|0*|[+]*)

Saya juga mencoba ini tetapi sebenarnya tidak berfungsi di Java tetapi hanya di Php jadi saya ingin bantuan untuk regix berbasis Java

^(?:\s*0+|[+]0*|(\d+)0*)(?!$)

Contoh Masukan

+490232345678
0049032345678
+1 (555) 234-5078
+7 (23) 45/6789+10
(0123) 345/5678, ext. 666

Keluaran yang diinginkan

490232345678
49032345678
15552345678
72345678910
1233455678666

Saya hanya perlu regix karena saya sudah tahu cara menggunakan regix itu di Java. Saya memiliki potongan kode ini yang membutuhkan regix

    String value = "+490232345678";
    Pattern p = Pattern.compile("(^\s*|0*|[+]*)");      
    Matcher m = p.matcher(value);
    value = m.replaceAll("");   
0
Mubasher 24 November 2017, 14:37

1 menjawab

Jawaban Terbaik

Coba ini:

public static void main(String[] args) {
    String [] testResult = {"+490232345678", 
                            "0049032345678", 
                            "+1 (555) 234-5078", 
                            "+7 (23) 45/6789+10", 
                            "(0123) 345/5678, ext. 666"};
    String reg = "^([\\(+ ]0| +|0+|\\(\\)|\\+)| +|[^\\d]+|/$";
    for (String phone : testResult) {
        System.out.println(phone.replaceAll(reg, ""));
    }
}

Keluaran akan menjadi:

490232345678
49032345678
15552345078
72345678910
1233455678666

Pendekatan yang lebih sederhana adalah melakukannya dalam dua langkah:

 .replaceAll("[^\\d]+", "").replaceAll("^0+", "")

Hapus semua bukan angka dan kemudian nol di depan.

Penjelasan RegEx

^([\(+ ]0| +|0+|\(\)|\+)| +|[^\d]+|\/$

 1st Alternative ^([\(+ ]0| +|0+|\(\)|\+)
     ^ asserts position at start of the string
   1st Capturing Group ([\(+ ]0| +|0+|\(\)|\+)
       1st Alternative [\(+ ]0
           Match a single character present in the list below [\(+ ]
           \( matches the character ( literally (case sensitive)
           +  matches a single character in the list +  (case sensitive)
           0 matches the character 0 literally (case sensitive)
       2nd Alternative  +
           + matches the character   literally (case sensitive)
           + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
       3rd Alternative 0+
           0+ matches the character 0 literally (case sensitive)
           + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
       4th Alternative \(\)
           \( matches the character ( literally (case sensitive)
           \) matches the character ) literally (case sensitive)
       5th Alternative \+
           \+ matches the character + literally (case sensitive)
 2nd Alternative  +
    + matches the character   literally (case sensitive)
    + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
 3rd Alternative [^\d]+
    Match a single character not present in the list below [^\d]+
    + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
    \d matches a digit (equal to [0-9])
 4th Alternative \/$
    \/ matches the character / literally (case sensitive)
 $ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)

Sumber Penjelasan: https://regex101.com/

Dan berikut adalah representasi visual dari https://www.debuggex.com/

enter image description here

2
Jorge Campos 24 November 2017, 12:20