Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Regex: how to capture multiple matches in the same line

Tags:

java

regex

I am trying to match a regex pattern in Java, and I have two questions:

  1. Inside the pattern I'm looking for there is a known beginning and then an unknown string that I want to get up until the first occurrence of an &.
  2. there are multiple occurrences of these patterns in the line and I would like to get each occurrence separately.

For example I have this input line:

1234567 100,110,116,129,139,140,144,146 http://www.gold.com/shc/s/c_10153_12605_Computers+%26+Electronics_Televisions?filter=Screen+Refresh+Rate%7C120HZ%5EScreen+Size%7C37+in.+to+42+in.&sName=View+All&viewItems=25&subCatView=true   ISx20070515x00001a          http://www.gold.com/shc/s/c_10153_12605_Computers+%26+Electronics_Televisions?filter=Screen+Refresh+Rate%7C120HZ&sName=View+All&subCatView=true 0   2819357575609397706

And I am interested in these strings:

Screen+Refresh+Rate%7C120HZ%5EScreen+Size%7C37+in.+to+42+in.

Screen+Refresh+Rate%7C120HZ
like image 864
Amit Avatar asked Sep 19 '11 11:09

Amit


People also ask

What is multiline pattern match?

Pattern. MULTILINE or (? m) tells Java to accept the anchors ^ and $ to match at the start and end of each line (otherwise they only match at the start/end of the entire string). Pattern.

What does capture mean in regex?

capturing in regexps means indicating that you're interested not only in matching (which is finding strings of characters that match your regular expression), but you're also interested in using specific parts of the matched string later on.

How do I match a pattern in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" .

How do you write more than one line in regex?

You can use alternation (|) operator to combine multiple patterns for your regexp. But in case you have various input and you will have to convert them to instance of Date from a string. Then you must follow in a sequence and validate the input one by one.


1 Answers

Assuming the known beginning is filter=**, the regular expression pattern (?:filter=\\*\\*)(.*?)(?:&) should get you what you need. Use Matcher.find() to get all occurrences of the pattern in a given string. Using the test string you provided, the following:

final Pattern p = Pattern.compile("(?:filter=\\*\\*)(.*?)(?:&)");
final Matcher m = p.matcher(testString);
int cnt = 0;
while (m.find()) {
    System.out.println(++cnt + ": G1: " + m.group(1));
}

Will output:

1: G1: Screen+Refresh+Rate%7C120HZ%5EScreen+Size%7C37+in.+to+42+in.
2: G1: Screen+Refresh+Rate%7C120HZ**
like image 167
Dan Cruz Avatar answered Oct 06 '22 01:10

Dan Cruz