Search Java Code Snippets

  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.

#Java - Code Snippets for '#Regex' - 6 code snippet(s) found

 Sample 1. Code to find email ids in a string using Pattern.

String text="vivek boy goy sdjs@adjk";
Pattern regex = Pattern.compile("[@]");
Matcher regexMatcher = regex.matcher(text);
int i =0;
int width = 0;
while (regexMatcher.find()) {
if((regexMatcher.start()-10 > 0) && (regexMatcher.end()+10 < text.length())){
String[] substr=text.substring(regexMatcher.start()-width,regexMatcher.end()+width).split(" ");
for(int j=0;j<substr.length;j++){
if(substr[j].contains("@") && (substr[j].contains(".com") || substr[j].contains(".net"))){
} else {

   Like      Feedback     regex  pattern  string  regexMatcher.find  java.util.regex.Matcher  java.util.regex.Pattern

 Sample 2. Java Code to get all images url from html code.

regex = Pattern.compile("[http]");
regexMatcher = regex.matcher(htmlParseData.getHtml());
List tr=htmlParseData.getOutgoingUrls();
if(tr.get(i).toString().contains(".jpg") || tr.get(i).toString().contains(".jpeg") || tr.get(i).toString().contains(".gif") || tr.get(i).toString().contains(".bmp")){
url = new URL(tr.get(i).toString());
Image image = new ImageIcon(url).getImage();

   Like      Feedback     regex  pattern  regex.matcher  html  java.util.regex.pattern  regular expressions

 Sample 3. Method to check if the WebUrl matches a Pattern.

public boolean shouldVisit(WebURL url) {
Pattern filters = Pattern.compile(".*(.(htm|html))$");

String href = url.getURL().toLowerCase();
return true;
return false;

   Like      Feedback     weburl  patternmatching  .getURL()  pattern  pattern.matcher  pattern.matcher.matches   regex  regular expression

 Sample 4. Card Number Validation using CodeValidator and RegexValidator

String CARD_REGEX = "^(5[1-5]d{2})(?:[- ])?(d{4})(?:[- ])?(d{4})(?:[- ])?(d{4})$";
CodeValidator validator = new CodeValidator(CARD_REGEX, LuhnCheckDigit.LUHN_CHECK_DIGIT);
RegexValidator regex = validator.getRegexValidator();

   Like      Feedback     Card Number Validation  Apache Commons  CodeValidator  LuhnCheckDigit.LUHN_CHECK_DIGIT  RegexValidator

Subscribe to Java News and Posts. Get latest updates and posts on Java from
Enter your email address:
Delivered by FeedBurner
 Sample 5. Code Sample / Example / Snippet of java.util.regex.Matcher

    private void defineVariables(String line) {

Matcher varDefn = matchesVarDefn.matcher(line);

if (varDefn.lookingAt()) {

String var =;

String val =;

vars.define(var, val);

} else {

String[] words = splitWords.split(line);

for (String var : words) {

String value = System.getenv(var);

vars.define(var, value);




   Like      Feedback      java.util.regex.Matcher

 Sample 6. Code Sample / Example / Snippet of java.util.regex.Pattern

    private long getStoreId(File storeFile) {

Pattern p = Pattern.compile(m_name + "-(\d+)");

Matcher m = p.matcher(storeFile.getName());

if (m.find()) {

return Long.parseLong(;


throw new RuntimeException("Invalid store file name: " + storeFile.getName());


   Like      Feedback      java.util.regex.Pattern

Subscribe to Java News and Posts. Get latest updates and posts on Java from
Enter your email address:
Delivered by FeedBurner