Skip to content

Instantly share code, notes, and snippets.

@klehmann
Last active May 5, 2022 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klehmann/bbac3fec7ab81e146f5791ebe4f72eb2 to your computer and use it in GitHub Desktop.
Save klehmann/bbac3fec7ab81e146f5791ebe4f72eb2 to your computer and use it in GitHub Desktop.
Regular expression to find German phone numbers
/*
* ==========================================================================
* Licensed under the Apache License, Version 2.0 (the "License"). You may
* not use this file except in compliance with the License. You may obtain a
* copy of the License at <http://www.apache.org/licenses/LICENSE-2.0>.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
* ==========================================================================
*/
package com.mindoo.contentregexp;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Utility class to extract German phone numbers from a given String via regular expression.<br>
*
* @author Karsten Lehmann, Mindoo GmbH
*/
public class PhoneNumberMatcher {
public static void main(String[] args) {
try {
PhoneNumberMatcher m = new PhoneNumberMatcher();
m.run();
System.exit(0);
}
catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
private void run() {
String str = "Lorem ipsum +49 (721) 46139940 "
+ "Lorem ipsum +49 (721) 461 399 40 "
+ "Lorem ipsum +49 (721) 461 399-40 "
+ "Lorem ipsum +49 (721) 461 399 - 40 "
+ "Lorem ipsum +49 (721)461 399-40 "
+ "Lorem ipsum +49 (721)461399-40 "
+ "Lorem ipsum (0721) 46139940 "
+ "Lorem ipsum (0721) 461 399 40 "
+ "Lorem ipsum (0721) 461399-40 "
+ "Lorem ipsum (0721) 461 399-40 "
+ "Lorem ipsum (0721) 461 399 - 40 "
+ "Lorem ipsum 0721/46139940 "
+ "Lorem ipsum 0721 / 46139940 "
+ "Lorem ipsum 0721 / 461399-40 "
+ "Lorem ipsum 0721 / 461 399-40 "
+ "Lorem ipsum 0721 / 461 399 - 40.";
Set<String> allMatches = findMatches(str);
System.out.println(allMatches.size()+" matches found");
allMatches.forEach((line) -> { System.out.println(line); });
}
public static Set<String> findMatches(String str) {
Set<String> allMatches = new LinkedHashSet<String>();
{
// +49 (721) 46139940
// +49 (721) 461 399 40
// +49 (721) 461 399-40
// +49 (721) 461 399 - 40
// +49 (721)461 399-40
// +49 (721)461399-40
Matcher m = Pattern.compile("(\\+\\d+[ ]+\\(\\d+\\)[ ]?[\\d -]+)")
.matcher(str);
while (m.find()) {
String currNumber = m.group().trim();
if (currNumber.length()>0) {
allMatches.add(currNumber);
}
}
}
{
// (0721) 46139940
// (0721) 461 399 40
// (0721) 461399-40
// (0721) 461 399-40
// (0721) 461 399 - 40
Matcher m = Pattern.compile("(\\(0\\d+\\)[\\d -]+)")
.matcher(str);
while (m.find()) {
String currNumber = m.group().trim();
if (currNumber.length()>0) {
allMatches.add(currNumber);
}
}
}
{
// 0721/46139940
// 0721 / 46139940
// 0721 / 461399-40
// 0721 / 461 399-40
// 0721 / 461 399 - 40
Matcher m = Pattern.compile("(0\\d+[ ]?/[ \\d-]+)")
.matcher(str);
while (m.find()) {
String currNumber = m.group().trim();
if (currNumber.length()>0) {
allMatches.add(currNumber);
}
}
}
{
// +49 (0)721 46139940
// +49 (0)721 461 399 40
// +49 (0)721 461 399-40
// +49 (0)721 461 399 - 40
// +49 (0)721461 399-40
// +49 (0)721461399-40
//
// +49 (0)721 / 46139940
// +49 (0)721 / 461 399 40
// +49 (0)721 / 461 399-40
// +49 (0)721 / 461 399 - 40
// +49 (0)721461 / 399-40
//
// +49 (0)721/46139940
// +49 (0)721/461 399 40
// +49 (0)721/461 399-40
// +49 (0)721/461 399 - 40
// +49 (0)721461/399-40
// +49 (0)721461/399-40
Matcher m = Pattern.compile("(\\+\\d+[ ]?\\(0\\)[\\d -/]+)")
.matcher(str);
while (m.find()) {
String currNumber = m.group().trim();
if (currNumber.length()>0) {
allMatches.add(currNumber);
}
}
}
return allMatches;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment