Home > Software design >  Match every single String after character in Java (RegEx)
Match every single String after character in Java (RegEx)

Time:12-12

I need help with this, so uhm... I have a String:

Hey ~firstname ~lastname today is the ~date

I want the program to print out this output:

firstname lastname date

in a list from the matcher (I already have a way of doing that). The only thing that I need is the regex to get my wanted output

Every help will be apprechiated

CodePudding user response:

Using a matcher with the pattern ~(\S ) we can try:

List<String> matches = new ArrayList<>();
String input = "Hey ~firstname ~lastname today is the ~date";
String pattern = "~(\\S )";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
while (m.find()) {
    matches.add(m.group(1));
}
String output = String.join(" ", matches);
System.out.println(output);  // firstname lastname date

CodePudding user response:

Nevermind guys, i got it working with this pattern:

(?<=~)\w 
  • Related