My pattern looks something like
<xxxx location="file path/level1/level2" xxxx some="xxx">
I am only interested in the part in quotes assigned to location. Shouldn't it be as easy as below without the greedy switch? Does not seem to work :(
/.*location="(.*)".*/
Im having a mysql query like this
SELECT group_name FROM t_groups WHERE group_name LIKE '%PCB%';
The results are
group_name
------------
PCB
Full size PCB
Another query,
SELECT group_name FROM t_groups WHERE group_name LIKE '%PCB-123%';
group_name
-----------
PCB-123
How can i use a query that will show all the three results ?,I mean i need to get all the results that starts or contains PCB
Hi All,
I want word '90%' to be matched with my String "I have 90% shares of this company".
how can I write regular expression for same?
I tried something like this:
Pattern p = Pattern.compile("\\b90\\%\\b", Pattern.CASE_INSENSITIVE
| Pattern.MULTILINE);
Matcher m = p.matcher("I have 90% shares of this company");
while (m.find()){
System.out.println(m.group());
}
but no luck.
Can any one thow some lights on this?
Many thanks,
Archi
I have model, Match, with two foreign keys:
class Match(model.Model):
winner = models.ForeignKey(Player)
loser = models.ForeignKey(Player)
When I loop over Match I find that each model instance uses a unique object for the foreign key. This ends up biting me because it introduces inconsistency, here is an example:
>>> def print_elo(match_list):
... for match in match_list:
... print match.winner.id, match.winner.elo
... print match.loser.id, match.loser.elo
...
>>> print_elo(teacher_match_list)
4 1192.0000000000
2 1192.0000000000
5 1208.0000000000
2 1192.0000000000
5 1208.0000000000
4 1192.0000000000
>>> teacher_match_list[0].winner.elo = 3000
>>> print_elo(teacher_match_list)
4 3000 # Object 4
2 1192.0000000000
5 1208.0000000000
2 1192.0000000000
5 1208.0000000000
4 1192.0000000000 # Object 4
>>>
I solved this problem like so:
def unify_refrences(match_list):
"""Makes each unique refrence to a model instance non-unique.
In cases where multiple model instances are being used django creates a new
object for each model instance, even if it that means creating the same
instance twice. If one of these objects has its state changed any other
object refrencing the same model instance will not be updated. This method
ensure that state changes are seen. It makes sure that variables which hold
objects pointing to the same model all hold the same object.
Visually this means that a list of [var1, var2] whose internals look like so:
var1 --> object1 --> model1
var2 --> object2 --> model1
Will result in the internals being changed so that:
var1 --> object1 --> model1
var2 ------^
"""
match_dict = {}
for match in match_list:
try:
match.winner = match_dict[match.winner.id]
except KeyError:
match_dict[match.winner.id] = match.winner
try:
match.loser = match_dict[match.loser.id]
except KeyError:
match_dict[match.loser.id] = match.loser
My question: Is there a way to solve the problem more elegantly through the use of QuerySets without needing to call save at any point? If not, I'd like to make the solution more generic: how can you get a list of the foreign keys on a model instance or do you have a better generic solution to my problem?
Please correct me if you think I don't understand why this is happening.
I'm trying to find near duplicate values in a set of fields in order to allow an administrator to clean them up.
There are two criteria that I am matching on
One string is wholly contained within the other, and is at least 1/4 of its length
The strings have an edit distance less than 5% of the total length of the two strings
The Pseudo-PHP code:
foreach($values as $value){
foreach($values as $match){
if(
(
$value['length'] < $match['length']
&&
$value['length'] * 4 > $match['length']
&&
stripos($match['value'], $value['value']) !== false
)
||
(
$match['length'] < $value['length']
&&
$match['length'] * 4 > $value['length']
&&
stripos($value['value'], $match['value']) !== false
)
||
(
abs($value['length'] - $match['length']) * 20 < ($value['length'] + $match['length'])
&&
0 < ($match['changes'] = levenshtein($value['value'], $match['value']))
&&
$match['changes'] * 20 <= ($value['length'] + $match['length'])
)
){
$matches[] = &$match;
}
}
}
I've tried to reduce calls to the comparatively expensive stripos and levenshtein functions where possible, which has reduced the execution time quite a bit.
However, as an O(n^2) operation this just doesn't scale to the larger sets of values and it seems that a significant amount of the processing time is spent simply iterating through the arrays.
Some properties of a few sets of values being operated on
Total | Strings | # of matches per string | |
Strings | With Matches | Average | Median | Max | Time (s) |
--------+--------------+---------+--------+------+----------+
844 | 413 | 1.8 | 1 | 58 | 140 |
593 | 156 | 1.2 | 1 | 5 | 62 |
272 | 168 | 3.2 | 2 | 26 | 10 |
157 | 47 | 1.5 | 1 | 4 | 3.2 |
106 | 48 | 1.8 | 1 | 8 | 1.3 |
62 | 47 | 2.9 | 2 | 16 | 0.4 |
Are there any other things I can do to reduce the time to check criteria, and more importantly are there any ways for me to reduce the number of criteria checks required (for example, by pre-processing the input values), since there is such low selectivity?
Hello !
I'm trying to extract some information from a webpage using php cURL+preg_match or any other function but for some reasons it doesn't work at all .
For example from this page http://www.foxtons.co.uk/search?location_ids=1001-29&property_id=712128&search_form=map&search_type=LL&submit_type=search
I want to extract the title which is "4 bed house to rent,
Caroline Place, Bayswater, W2", the price which is "2,300" and the description which starts at "This fantastic..." and ends at "(Circle and District Lines). "
I tried to use php cURL + dom but I'm getting a lot of errors like this "htmlParseEntityRef: expecting ';' in Entity, line: 243" and no result displayed
Also I tried to use preg_match or preg_match_all but doesn't work either .
A very basic example would be highly appreciated !
thank you !
Hello
I am applying the following function
<?php
function replaceChar($string){
$new_string = preg_replace("/[^a-zA-Z0-9\sçéèêëñòóôõöàáâäåìíîïùúûüýÿ]/", "", $string);
return $new_string;
}
$string = "This is some text and numbers 12345 and symbols !£%^#&$ and foreign letters éèêëñòóôõöàáâäåìíîïùúûüýÿ";
echo replaceChar($string);
?>
which works fine but if I add ã to the preg_replace like
$new_string = preg_replace("/[^a-zA-Z0-9\sçéèêëñòóôõöàáâãäåìíîïùúûüýÿ]/", "", $string);
$string = "This is some text and numbers 12345 and symbols !£%^#&$ and foreign letters éèêëñòóôõöàáâäåìíîïùúûüýÿã";
It conflicts with the pound sign £ and replaces the pound sign with the unidentified question mark in black square.
This is not critical but does anyone know why this is?
Thank you,
Barry
I am using TexMaker of Karmic Ubuntu with Okular. I use pdfLatex to compile and I view the PDFs in Okular. How can I configure Okular to inverse search with TexMaker? I have tried the following code:
texmaker %f -line %l
but it does not work. I have tried double clicking, ctrl+click, shift+click, ctrl+shift+click, ctrl+alt+click, alt+shift+click, still nothing. Perhaps I simply don't know what the action is to initiate the inverse search from within Okular.
How can I configure Okular to inverse search with TexMaker?
Hi all,
I'm using the function preg_replace but I cannot figure out how to make it work, the function just doesn't seem to work for me.
What I'm trying to do is to convert a string into a link if any word contains the % (percentage) character.
For instance if I have the string "go to %mysite", I'd like to convert the mysite word into a link.
I tried the following...
$data = "go to %mysite";
$result = preg_replace('/(^|[\s.\,\:\;]+)%([A-Za-z0-9]{1,64})/e', '\1%\2', $data);
...but it doesn't work.
Any help on this would be much appreciated.
Thanks
Juan
I have a simple commenting system here...
http://affbuzz.com/comments/7299a55137def55917a5dc6c4fe0f261af8a4217
...and people can submit hyperlinks inside the plain text field. When I display these records back from the database and into the web page, what RegExp in PHP can I use to convert these links into HTML-type anchor links?
Bonus: For the algorithm to not do this with any other kind of link, just http and https.
Hello!
I load an external xml file containing user metas looking like this:
<?xml version="1.0" encoding="utf-8"?>
<user_information>
<user url="http://usersweb.com">
<name>Arnold</name>
<lastname>
<name>Arnoldson</name>
</lastname>
<age>42</age>
</user>
<user url="http://anotheruserweb.com">
<name>Arnold</name>
<lastname>
<name>Arichson</name>
</lastname>
<age>42</age>
</user>
And so on....
</user_information>
I know the formatting with
<lastname>
is really stupid, but i can't modify the source.
I want to load in the "url"-attribute from
<user>
, but only if
<name>
and
<lastname>
matches the name values i have stored in my own user database.
I know how to parse the rest of it with PHP, but just the matching sequence seems difficult to me. Any help appreciated!
Hey all! I currently have a preg_match to detect http:// and www. etc..... but I want to detect domain.com or domain.co.uk etc etc...
Sorry... to clarify: i'm detecting in a string.
Any ideas i need to scratch up on my regex!
Thanks,
Stefan
I'm generating an XML from data that comes from database (and some JSON feeds).
I'm having some problems with some texts that contains some hex chars that are breaking my XML.
For example, see this screenshot of the error I get from Chrome:
I identified the hex characters that are giving me problems (I believe they're called control characters). And these are:
0x03
0x05
0x16
0x0E
How can I replace those characters with PHP before printing them on my XML output?
Thanks!
e.g
string = "This is a re@lly long long,long! sentence";
becomes
string = "This is a long sentence";
Basically so all non-alphanumeric words or removed keeping spaces in tacked
Any ideas?
I'm trying to extract some specific pictures from html content . Currently I have the following array
[1] = Array
(
[0] = BuEZm5g!Wk~$(KGrHqQH-DgEvrb2CLuOBL-vbkHlKw~~_14.JPG#!BuEZm5g!Wk~$(KGrHqQH-DgEvrb2CLuOBL-vbkHlKw~~_12.JPG|1#http://i.ebayimg.com/08#!BuE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N!EWk~$(KGrHqUH-C0EvsBdjbv0BL-vdeFkD!~~_12.JPG|10#http://i.ebayimg.com/16#!BuE,IOgBmk~$(KGrHqEH-EMEvpym7mSLBL-vdqI6nw~~_14.JPG#!BuE,IOgBmk~$(KGrHqEH-EMEvpym7mSLBL-vdqI6nw~~
)
I need to extract only the pictures that start with ! and ends in 12.JPG . An example of expected result is
!BuEZm5g!Wk~$(KGrHqQH-DgEvrb2CLuOBL-vbkHlKw~~_12.JPG
. I have a pattern but it doesn't work for some reasons that I don't know . It is
$pattern = "/#\!(.*)_12.JPG/";
Thank you in advance for any help
Write a JAVA program that calculates the surface area of a cylinder, C= 2( p r2) + 2 p r h, where r is the radius and h is the height of the cylinder. Allow the user to enter in a radius and a height. Format the output to three decimal places. Use the constant PI and the method pow() from the Math class.
This is what I've done so far but it can not run. Can anyone help me ?
import Java.util Scanner;
public class Ex5 {
public static void main (String[] args)
{
Scanner input.new Scanner(System.in);
double radius,height,area;
System.out.print("Please enter the radius of the Cylinder");
radius = input.nextDouble();
System.out.print("Please enter the height of the Cylinder");
height = input.nextDouble();
area = (4*Math.PI*radius)+2*Math.PIMath.POW(radius,2);
System.out.println("The surface of the cylinder" + area);
}
}
I have a CMS content in database. The content contains '<img src= .............../>' also.
I want to retrieve this content using mysql query and show on frontend but with all '<img src='.........../>' removed from the content.
How can it be done using query in mysql?
I need to find the value of a variable and use it to add a class to a div, based on a switch statement.
For example, my variable is $link and if $link has google.com IN IT at all, I need $class to equal 'google', if $link as yahoo.com IN IT at all, $class then needs to equal 'yahoo'
So, I need something like this, but I'm not sure how/or if to use preg_match or something to check and see if the $link variable has the value we are looking for in it - see 'case' text below:
switch ($link) {
case 'IF link has Google.com in it':
$class = 'google';
break;
case 'IF link has Yahoo.com in it':
$class = 'yahoo';
break;
default:
# code...
break;
}
OR if there is a better way to do this, please let me know :D
Thanks
I'm trying to strip content titles out of the middle of text strings. Could I use regex to strip everything out of this string except for the title (in italics) in these strings? Or is there a better way?
Joe User wrote a blog post called
The 10 Best Regex Expressions in the category Regex.
Jane User wrote a blog post called
Regex is Hard! in the category TechProblems.
I've tried to come up with a regex expression to cover this, but I think it might need two. The trick is that the text in bold is always the same, so you could search for that, like this:
regex: delete everything before and including wrote a blog post called
regex: delete in the category and everything after it.
Trying to parse a csv file that has all the data wrapped in double quotes, because there may be commas in the double quotes.
Looks like this:
$songs = '"1, 2, 3, 4 (I Love You)","Plain White T's","CBE10-22",15,"CBE10-22","","","CB",984,"","10/05/10"';
$regResult = preg_match( "", $songs, $matches );
I can't figure out a regex that will return the data between the quotes as the matches. I'm sure there is some regex master that can help me with this.
Hi,
On OS-X (PHP5.2.11) I have a file: siësta.doc (and thousand other with Unicode filenames) and I want to convert the file names to a web-consumable format (a-zA-Z0-9.). If I hardcode the file name above I can do the right conversion:
<?php
$file = 'siësta.doc';
echo preg_replace("/[^a-zA-Z0-9.]/u", '_', $file);
// Output: si_sta.doc
?>
But if I read the file names with scandir, I've got strange conversions:
<?php
$files = scandir(DIRNAME);
foreach ($files as $file) {
echo preg_replace("/[^a-zA-Z0-9.]/u", '_', $file);
// Output for the file above: sie_sta.doc
}
?>
I tried to detect the encoding, set the encoding, convert it with iconv functions. I tried the mb_ functions also. But it was just worse.
What did I do wrong?
Thanks in advance
Hi All!
EDITED:
need help on split Array
array example:
$text = array (
[0] =>
TEXT1:some normal text
TEXT2:some long text here, and so on... sometimes
i'm breaking down and...
TEXT3:some normal text
TEXT4:some normal text
[1] =>
TEXT1:some normal text
TEXT2:some long text here, and so on... sometimes
i'm breaking down and...
TEXT3:some normal text
TEXT4:some normal text
)
ok, now by using
preg_split( '#\n(?!s)#' , $text );
i get
[0] => Array
(
[0] => some normal text
[1] => some long text here, and so on... sometimes
[2] => some normal text
[3] => some normal text
)
As you can see the [1] Element of the Array is cutted off!
i want keep this line entirely:
some long text here, and so on...
sometimes i'm breaking down and...
what Regex can get the entire line and also split at line break!?
I am trying to take a string of text like so:
$string = "This (1) is (2) my (3) example (4) text";
In every instance where there is a positive integer inside of parentheses, I'd like to replace that with simply the integer itself.
The code I'm using now is:
$result = preg_replace("((\d+))", "$0", $string);
But I keep getting a "Delimiter must not be alphanumeric or backslash" error.
Any thoughts? I know there are other questions on here that sort of answer the question, but my knowledge of regex is not enough to switch it over to this example.