2

I am using regex patterns to search this string:

<tr><td>111</td><td>111@test.com</td></tr><tr><td>222</td><td>222@test.com</td></tr></table>

But always get matcher result as false, any suggestions why?

I want to substring value between <tr> and </tr>.

Pattern myPattern = Pattern.compile('\\<tr>(.*?)\\</tr>');
Matcher m = myPattern.matcher(temp);
while(m.find())
{
  .. do things
}

And also this one still not working <tr>(.*?)</tr>. I check with http://regexr.com/, but it seem ok. However, when I run the code, not working.

Original Post: https://stackoverflow.com/questions/23924814/substring-with-regex-for-between-tr-tag/23924891#23924891

  • Have you tried escaping the > character too? – Boris Bachovski May 29 '14 at 2:54
  • Is the issue in not returning the <td> tags? – Boris Bachovski May 29 '14 at 3:02
  • Hi , the issue is even I use <tr>(.*?)</tr> to compile the pattern. The Matcher m can't find the matching one. In this case, should be true as <tr></tr> is included in input string. Now always returning false. – kitokid May 29 '14 at 3:05
  • I have just tested this and it worked, it returned 111111@test.com and 222222@test.com – Boris Bachovski May 29 '14 at 3:06
3

I have executed the following code:

String temp = '<tr><td>111</td><td>111@test.com</td></tr><tr><td>222</td><td>222@test.com</td></tr></table>';
Pattern myPattern = Pattern.compile('<tr>(.*?)</tr>');
Matcher m = myPattern.matcher(temp);
while(m.find())
{
    system.debug(m.group());
}

and I got back:

13:07:45.032 (32786697)|USER_DEBUG|[6]|DEBUG|111111@test.com
13:07:45.032 (32915341)|USER_DEBUG|[6]|DEBUG|222222@test.com
  • 1
    yah. I think seem like the code itself is correct. I think problem lies on the value of table itself. I will check the value because the value I provided here is just sample. – kitokid May 29 '14 at 3:18
  • 1
    it was not the coding issue. it was the value issue which has hidden character. So when it was removed the problem solved. – kitokid May 29 '14 at 6:47
4

It's generally not advised to try and parse HTML with a regular expressions. There's a number of potential problems, but nested tags is one of the big ones. If that advice is applicable here depends on the possible values your code needs to accept.

Apex's ability to work with XML as a document model is solid, but searching that tree is something that you'd have to build support for sadly. This means you'd end up writing a lot of code, and in the end it would quite possibly perform worse (a guess, partially due to the reflection call overhead of the runtime). If you do have to go down the XML parsing route there's two main options:

  1. Dom.Document and Dom.XMLNode. These let you navigate between XML nodes, descending to children, back up, or really anywhere. You're free to hop around the tree processing it, but that means recursion or similar to handle it - and apex has a relatively small stack depth.

  2. XmlStreamReader has a loop-based approach of reading the document as it goes, which means no going back up to a parent. The upside is this is probably much less code, and conceptually simpler than traversing an XML node tree. I'll also speculate wildly that it's much more efficient on heap use.

Based on that I'd advise sanity checking if regex is the right tool for the job, hoping it is since it's clearly much simpler to use here. If it's not I'd take a good hard look at using XMLStreamReader to parse.

There are a few upsides to XML parsing though, particularly that it's quite robust compared to handling the document fragment as text. It should handle all sorts of variations, like <td />, odd whitespace, attributes, and all the other goodies that XML supports.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.