I have strings like
S103 FYUN031800644100
5 S103 LKAAN031801261400
32 S103 AKJA031804534600
From the above strings, I have to extract the bolded part from each of the above strings using regex, anyone with an idea on how to go about it?
So far I have tried this
String acc = "S103L5AN031801261400";
String regex = ".*S(.{3})";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(acc);
while(matcher.find()){
Logger.debug("Match:", matcher.group());
}
But still, I can't get it right.
You can use the regex, [A-Za-z]\\d{3}
which means an alphabet followed by 3 digits.
If you want the substring to start with S
or s
only, use [Ss]\d{3}
or (?i)s\\d{3}
instead. Note that (?i)
makes the pattern case-insensitive.
If you want the substring to start with S
only, use S\d{3}
instead.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Stream.of(
"S103FYUN031800644100",
"5S103LKAAN031801261400",
"32S103AKJA031804534600"
).forEach(s -> System.out.println(s + " => " + getSubstr(s)));
}
static String getSubstr(String s) {
Matcher matcher=Pattern.compile("[A-Za-z]\\d{3}").matcher(s);
String substr="";
if(matcher.find()) {
substr = matcher.group();
}
return substr;
}
}
Output:
S103FYUN031800644100 => S103
5S103LKAAN031801261400 => S103
32S103AKJA031804534600 => S103
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.