java regex/arraylist issue, unable to get matches/arraylist store new values

just getting into java arrays and regex and trying to build a program I once previously built in php, what I need to happen is I have an arraylist from twitter that I would like to use regular expressions to find text containing links, if a text contains links I want to add these new values to a new array which I would like to display, however the final array returns empty, meaning that at some point either the regular expression in my code isnt matching properly or the values arent transferring over to the new array, as I am new to this in java I am unable to spot where this is going wrong, any help would be massive, thanks in advance.

protected void onPostExecute(ResponseList<twitter4j.Status> results) { // TODO Auto-generated method stub super.onPostExecute(results); ArrayList<twitter4j.Status> al = new ArrayList<twitter4j.Status>(); for(twitter4j.Status statii: results){ String patternStr = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"; Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(statii.getText()); if(matcher.find() == true){ al.add(statii); } } StatusListAdapter adapter = new StatusListAdapter( TweepicsappActivity.this, al); setListAdapter(adapter); }


Your regex has a bunch of unescaped special-characters in it (i.e. things like + and , and . and : and | that have meaning to the regex parser and do not match literal text unless you escape them).

Personally I always escape all special characters in a regex, even for ones that have no special meaning to the regex parser. The issues caused by forgetting to escape one can be too confusing to debug for it to be worth risking not escaping something, in my opinion.

So I would do patternStr like:

String patternStr = "(https?|ftp|file)\\://[\\-a-zA-Z0-9\\+\\&\\@\\#/\\%\\?\\=\\~\\_\\|\\!\\:\\,\\.\\;]*[\\-a-zA-Z0-9\\+\\&\\@\\#/\\%\\=\\~\\_\\|]";

Not very pretty, but it gets the job done.

Here's an example:

First, I would double check your regex. Step through the code in the debugger. Second, I would use matcher.matches() rather than find(). Lastly, if performance is important, I would reuse both the Matcher -- initializing it as a static member in a static class initializer.

private static final Matcher matcher;

static {
String patternStr = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
Pattern pattern = Pattern.compile(patternStr);
matcher = pattern.matcher("");

protected void onPostExecute(ResponseList results) {
// TODO Auto-generated method stub
ArrayList al = new ArrayList();

for(twitter4j.Status statii: results) {
if(matcher.matches()) {

StatusListAdapter adapter = new StatusListAdapter(TweepicsappActivity.this, al);

Category:java Time:2012-02-13 Views:1

Related post

  • Java Regex Multiline issue 2015-03-17

    I have a String read from a file via apache commons FileUtils.readFileToString, which has the following format: <!--LOGHEADER[START]/--> <!--HELP[Manual modification of the header may cause parsing problem!]/--> <!--LOGGINGVERSION[2.0.

  • Java regex: Parsing a string with two matching substrings 2012-02-13

    I have following string task [email protected]{taskId} "@{BLABLA.title}" and want to extract all placeholders from it. Placeholders are @{taskId} and @{BLABLA.title}. I use following code: final Pattern pattern = Pattern.compile(".*(\\@\\{.*?\\}).*"); final Ma

  • Unable to open Windows 8 Store 2012-10-11

    I updated from Windows 7 Ultimate (64 bit) to Windows 8 Pro (64 bit) and the "Store" app will not launch. I click the "Store" button, it visibly depresses, and then nothing happens. I have done the following steps to diagnose the problem: * Removed A

  • Java regex: Get the matched sequence 2010-06-29

    In Perl/PHP regex is's possible to match a sequence and get an array with matched sequences: preg_match('/20[0-1][0-9]/', $inputstring, $array_return); // PHP I can't figure out how to do this in Java. returns the whole string. Is this

  • How can I match series of | with a Java regex? 2010-09-26

    Couple of questions: 1) How to make the following regex which are based on search literal ^ work for the search literal | search literal ^ based regex (which works fine, which is one of the valuable inputs from this forum): String intermediateResult

  • Java regex String.matches working inconsistently 2012-04-12

    I have a regex which checks if a string is a number. The format's thousand separator is a white space, decimal separator is a dot. After-decimal part is optional. The issue is that at some point String.matches() function stops working as expected. Wh

  • Find out which group matches in Java regex without linear search? 2009-07-24

    I have some programmatically assembled huge regex, like this (A)|(B)|(C)|... Each sub-pattern is in its capturing group. When I get a match, how do I figure out which group matches without linearly testing each group(i) to see it returns a non-null s

  • In a Java regex with Quantifiers how can I obtain the matched groups? 2009-08-23

    I am processing text using Java Regexes (1.6) which contain quantifiers and I wish to return the number and values of matched groups. A simple example is: A BC DEF 1 23 456 7 XY Z which is matched by: ([A-Z]+){0,9} (\d+){0,9} ([A-Z]+){0,9} How can I

  • How can we match a^n b^n with Java regex? 2010-09-04

    This is the second part of a series of educational regex articles. It shows how lookaheads and nested references can be used to match the non-regular languge anbn. Nested references are first introduced in: How does this regex find triangular numbers

  • Replacing character with File.separator using java.regex Pattern Matcher 2011-01-22

    I have a field called 'path' in back end database, which stores the path to certain resource. Instead of storing lots of backslashed (escaped) path for windows path, my idea is to let user enter the path with certain character as file separator (inde

  • Need java regex to match the below string 2011-02-01

    What is the java regex to match the below pattern? <anyString>.<5 or 10>.anyNumber.anyNumber Here 5 and 10 are numbers. --------------Solutions------------- .*\.(?:5|10)\.\d+\.\d+ should work. Explanation: .* # any number of characters (e

  • Using Java regex want to match only & but not & < etc 2011-02-16

    Using Java regex pattern I want to match the & symbol. But it should not match &amp;, &lt; etc... (HTML encoding). --------------Solutions------------- Try this: &(?![A-Za-z]+;|#[0-9]+;) This matches an & that's not followed by on

  • Java Regex won't match, any explanations? 2011-04-25

    The regex String : "[Ff][uU][Nn][Cc] " Matches input: "fUnC " But not: "func across( a, b )" And I don't understand why... I'm testing my expressions here: I figured out that I (dumbly) needed my regex to

  • Matching Multiple Patterns using Java Regex 2011-05-11

    I have a file containing records of the following format: 1285957838.880 1 TCP_HIT/200 1434 GET [02/Oct/2010:00:00:38 +0530] je02121 NONE/- text/css Which has 11 fields ([02/Oct/2

  • How do I match latin unicode characters in ColdFusion or Java regex? 2011-07-01

    I'm looking for a ColdFusion or Java regex (to use in a replace function) that will only match numbers [0-9], letters [a-z], but include none ASCII Portuguese letters (unicode latin, like ç and ã). Some like this: str = reReplaceNoCase(str, "match no

  • Java regex matches only if matching is checked 2011-08-12

    Inside of a class I have a pattern private Pattern lossWer = Pattern.compile("^\\d+ \\d+ (\\d+).*"). One of the functions looks like this: public double[] getWer(){ double[] wer = new double[someStrings.size()]; Matcher m; for(int i = 0; i < wer.l

  • Java Regex pattern matching 2011-08-23

    I am developing a text highlighter in a file using the java regex pattern matching. Following is a code snapshot of it SearchQuery=preprocessedModifiedArrayList.get(i)+[\\w\\s\\W]*?"; pattern = Pattern.compile(SearchQuery); Matcher matcher = pattern.

  • How to match letters only using java regex, matches method? 2011-10-23

    import java.util.regex.Pattern; class HowEasy { public boolean matches(String regex) { System.out.println(Pattern.matches(regex, "abcABC ")); return Pattern.matches(regex, "abcABC"); } public static void main(String[] args) { HowEasy words = new HowE

  • How to match \Q and \E in Java regex? 2012-01-19

    I want to match \Q and \E in a Java regex. I am writing a program which will compute the length of the string, matching to the pattern (this program assumes that there is no any quantifier in regex except {some number}, that's why the length of the s

Copyright (C), All Rights Reserved.

processed in 0.430 (s). 13 q(s)