/ W3SCHOOLS

W3schools - RegEx

이 페이지는 다음에 대한 공부 기록입니다
Lecture에서 배웠던 내용을 복습하며 작성했습니다

찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요

Regular Expressions

Is a sequence of characters that forms a search pattern

When you search for data in a text, you can use this pattern to describe what you searching for

Find out if there are any occurrences of the word “w3schools” in a sentence:

import java.util.regex.Matcher;	// Used to search for the pattern
import java.util.regex.Pattern;	// Defines a pattern
// another class(PatternSyntaxException) : Indicate syntax error in a regular expression pattern
public class Main {
  public static void main(String[] args) {
// “w3schools” is being searched for in a sentence
    Pattern pattern = Pattern.compile("w3schools", Pattern.CASE_INSENSITIVE);
// create pattern, use the Pattern.compile() method
// first parameter indicates which pattern is being searched for
// second parameter(optional) has a flag to indicates that the search should be case-insensitive
    Matcher matcher = pattern.matcher("Visit W3Schools!");
// matcher() method is used to search for the pattern in a string
// It returns Matcher objext which contains information about the search that was perfromed
    boolean matchFound = matcher.find();
// It returns true if the pattern was found in the string
    if(matchFound) {
      System.out.println("Match found");
    } else {
      System.out.println("Match not found");
    }
  }
}
// Output will be Match found

Flags

In the compile() method change how the search is performed

  • Pattern.CASE_INSENSITIVE : The case of letters will be ignored

  • Pattern.LITERAL : Special characters in the pattern will not have any special meaning

  • Pattern.UNICODE_CASE : Use it together with the CASE_INSENSITIVE flag to also ignore the case the case of letters outside of the English alphabet

Patterns

[abc] : Find one character from the options between the brackets

[^abc] : Find one character NOT between the brackets

[0-9] : Find one character from the range 0 to 9

Metacharacters

Are characters with a special meaning

| : Find a match for any one of the patters separated by | as in: cat|dog|fish

. : Find just on instance of any character

^ : Finds a match as the beginning of a string as in: ^Hello

$ : Finds a match at the end of the string as in: World$

\d : Find a digit

\s : Find a whitespace character

\b : Find a match at the beginning of a word: \bWORD, or at the end of a word: WORD\b

\uxxxx : Find the Unicode character specified by the hexadecimal number xxxx

Quantifiers

Define quantities

n+ : Matches any string that contains at least one n

n* : Matches any string that contains zero or more occrrences of n

n? : Matches any string that contains zero or one occurences of n

n{x} : Matches any string that contains a sequence of X n’s

n{x,y} : Matches any string that contains a sequence of X to Y n’s

n{x,} : Matches any string that contains a sequence of at least X n’s