How to use a regular expression to extract a substring? - regex

so im trying to figure out regular expressions in Flex, but cant for the life of me figure out how to do the following.
From the sample below, i need to extract out only "Mike_Mercury".
So i have to somehow strip out everything around it with RegExp, or whatever's best. Also, I would need it to work with other samples as well. Im getting this from the reddit api, so id have to extract that same section from a whole bunch of these. Thanks!
<table>
<tr>
<td>
<a href="http://www.reddit.com/r/atheism/comments/q2sfe/barack_obamas_insightful_words_on_abortion/">
<img src="http://d.thumbs.redditmedia.com/9StfiHi7hEbf8v73.jpg" alt="Barack Obama's insightful words on abortion"
title="Barack Obama's insightful words on abortion" /></a>
</td>
<td>
submitted by Mike_Mercury
to atheism
<br />
[link] <a href="http://www.reddit.com/r/atheism/comments/q2sfe/barack_obamas_insightful_words_on_abortion/">
[1722 comments]</a>
</td>
</tr>
</table>

Try this regex:
submitted by (.*?)

Related

Collect results from groups to one string

I want to parse weather html page for Openhab.
This is significant part of whole html:
<!-- Amount of Sun -->
<tr>
<td class="label_det">
<span class="sum">∑</span> <span class="unit">in u</span>
</td>
<td class="sunamount">
10.2
</td>
<td class="sunamount">
10.6
</td>
<td class="sunamount">
5.9
</td>
<td class="sunamount">
6.8
</td>
<td class="dgrey sunamount">
6.8
</td>
<td class="dgrey sunamount">
5.4
</td>
<td class="sunamount">
5
</td>
</tr>
I would like to collect all numbers into one string, I understand that it's, perhaps, not possible, but may be...
So something like this: '10.2 10.6 5.9 6.8 6.8 5.4 5'
Example of full html and my current regex is here: https://regex101.com/r/nrzPHU/1
Thanks in advice.
You need named capture groups. Named capture groups allow you to specify a given part in regex with a name to extract it later. A named capture group starts with (? then followed by the regex and ended with ).
<td class\=\".*?sunamount\">\s+(?<amount>\d+(\.\d+)?)\s+<\/td>
You would then be able to extract the amount by applying your regex to the input and picking the group named amount out of it.
Reading about OpenHab online I'm not sure they support named capture groups. So an alternative would be using the regex above to match all lines with amounts in the input. Then using a regex replace on that matched string. So something like...
Use this regex to get amounts:
<td class\=\".*?sunamount\">\s+\d+(\.\d+)?\s+<\/td>
Use this regex on the result of the regex above to replace the non amounts (and replace them with an empty string to delete them):
([\s]|<td class=".*?">|<\/td>)

Please suggest correct regular expression for below situation

I have following response section.
06S
</td><td>
<img src="/Media/Images/Opr/2.png" title="Pa" />
</td><td style="0">
4
</td>
I want to extract 06S and 2 only if value in between tag is NOT 0.
I wrote following regular expression but it does not work. could anyone please help.
(?s)(.+?)
</td><td>
<img src="/Media/Images/Opr/(.+?).png" title="(.+?)" />
</td><td style="0">
([1-9]{1})
</td>
I didn't put a lot of time into making this pretty or anything but this will do what you asked.
<a href="\/Public\/Details\/(.+?)\?OID=(.+?)" .*?>.*?<\/a>\s*<\/td><td>\s*<img.*?\/>\s*<\/td><td.*?>\s*(?:[^\s0]|[^\s]{2,})?\s*<\/td>
This assumes that your data is coming in an extremely similar way that you posted in the question. I'm a little preoccupied so I couldn't make it much better.
obligatory regex101 link

Find a table's last cell by regular expression

I want to use Regular Expression (compatible with pcre) to select a table
cell in an XML or HTML file.This cell was expanded in several lines containing
other elements and relative attributes and values. Thiscell supposed to be at the last column.
for some reasons I can't and don't want to use ". matches newline" option.
for example in this code:
EDITED:
<table colcount="4">
<tr>
<td colspan="2">
<para><text> Mike</text></para>
</td>
<td>
<tab />
</td>
<td1>
<para><text>Jack</text></para>
<para><text>Sarah</text></para>
</td>
</tr1>
<tr>
<td>
<para><text>Bob</text></para>
<para><text>Rita</text></para>
</td>
<td2 colspan="3" with>
<para><text>Helen</text></para>
</td>
</tr2>
<tr>
<td style="with:445px;">
<para><text>Sam</text></para>
</td>
<td>
<para><text>Emma</text></para>
<para><text>George</text></para>
</td>
<td>
</td>
<td3 colspan="">
<tab />
</td>
</tr3>
</table>
/EDITED
I want to find and select the whole last cell together with its start and end tags (<td and </td>)
and the end tag of the corresponding row(</tr>), that is:
EDITED:
Here is what I want to select in the table like above using RegEx:
Either from <td1 to </tr1> - or from <td2 to </tr2> - or from <td3 to </tr3>
/EDITED
The format (indentation and new lines have to be preserved), I mean I can't put, for example
</tr> in front of of closing tag of the cell(</td>).
Indentation is only space character.
Thanks for any help...
Best you can do with regex is:
<td(([^<]|<(?!\/td>))*)<\/td>\s*<\/tr>(?!(.|\r|\n)*<tr)
But this is kinda ugly, resource intensive and breaks when you have nested tables. A better route is indeed to use an XML or HTML parser for whichever programming language you're using.
If you want to select the last cell from EVERY row, as your updated question suggests, leave out the negative lookahead like so:
<td(([^<]|<(?!\/td>))*)<\/td>\s*<\/tr>
Working example here: http://refiddle.com/gt2

Regex, wordpress, and Search Regex plugin - removing affiliate links

I have a wordpress blog(s), in which I am trying to use plugin named 'Search Regex' to remove a part of post text, that I've placed as advertisement on the bottom (don't ask)
Now, I'm spinning around for a few months trying to find a proper answer on this, I know mysql doesn't support regex fully, and it would probably be painful to even try doing it that way, so I decided to use this plugin.
My wordpress blog have couple of thousands of posts, with almost the same code on the bottom, and code looks something like this:
<!--more-->
<br />
<center>
<table width="100%">
<tbody>
<tr>
<td bgcolor="#000000" style="text-align: center; font-size: 16px; font-weight: bold;">
<a href="http://myaffiliate.com/?q2=affiliateid" target="_blank" rel="nofollow" ><img title="blabla" src="http://someimage.com/somewhere></a><br />
<b>Some random Blah</b>
</td>
</tr>
</tbody>
</table>
</center>
Everything outside is fixed and doesn't change, and everything inside tags is changing with each post.
Now, this is multiline text, and I SERIOUSLY have problem finding a matching string, and even tool, that could help me solving this puzzle. I have this spreaded on several blogs, and I figured that instead of spamming my blog with same repetitive code that makes 1/3rd of all content I have in my posts, I can simply include it through single.php
So, I want this permanently deleted from my database.
Thanks in advance for help.
I'm not an expert on blogs or regex, but couldn't you use a python script to substitute all that text to replace it?
Something like this
re.sub(r' <!--more-->
<br />
<center>
<table width="100%">
<tbody>
<tr>
[\.]+
</td>
</tr>
</tbody>
</table>
</center>
', "")
If I'm not wrong, that should find all the cases where it finds an ad and replace it with a blank string :)
You could try something like this (always take a backup before testing)
global $wpdb;
$posts=$wpdb->get_results("SELECT ID,post_content FROM {$wpdb->posts} LIMIT 0,1");
foreach($posts as $p){
$pos = strpos($p->post_content, '<!--more-->');
$more_content = substr($p->post_content,$pos);
if($pos !== false && strstr($more_content,"myaffiliate.com" && $p->ID>0)){
$content = substr($p->post_content,0,$pos-1);
$wpdb->query($wpdb->prepare("UPDATE {$wpdb->posts} SET post_content='%s' WHERE ID=%d",$content,$p->ID));
}
}
This is untested, but you get the idea.
This will remove the post content part after <!--more--> if it contains the string "myaffiliate.com" (this is somewhat lazy matching, but we could refine it with preg_match or preg_replace if you need it).
You could use LIMIT 0,1 while testing and then increase it to your needs.

Notepad++ search & replace

I'm trying to convert a html file with 100 of entries like this one:
<table>
<tr>
<td valign="top" width="30">
1.</td>
<td>
TEXT DESCRIPTION
</td>
</tr>
</table>
<table><tr><td></td></tr></table>
where the number "1." goes from 1 to 100, into this:
<li>
TEXT DESCRIPTION
</li>
I haven't find a way to do this, neither with regexp nor with extended search mode. Any ideas?
You could start with this:
Replace
.*<td>(.*[A-Za-z]+.*)<\/td>.*
with
<li>\1</li>
This will match one chunk of code of the form you reported. You must modify it to match multiple chunks of the same form in the same file.
Moreover to work correctly we should make it match lazily. Someone who knows how?