Regexp in Java
I want to make a regexp who do this
verify if a word is like [0-9A-Za-z][._-'][0-9A-Za-z]
example for valid words
A21a_c32
daA.da2
das'2
dsada
ASDA
12SA89
non valid words
dsa#da2
34$
Thanks
I want to replace all "mailto:" links in html with plain emails.
In: text .... <a href="mailto:[email protected]">not needed</a> text
Out: text .... [email protected] text
I did this:
$str = preg_replace("/\<a.+href=\"mailto:(.*)\".+\<\/a\>/", "$1", $str);
But it fails if there are multiple emails in string or html inside "a" tag
In: <a href="mailto:[email protected]">not needed</a><a href="mailto:[email protected]"><font size="3">[email protected]</font></a>
Out: [email protected]">
I have a table of contacts and a table of postcode data.
I need to match the first part of the postcode and the join that with the postcode table... and then perform an update...
I want to do something like this...
UPDATE `contacts` LEFT JOIN `postcodes` ON PREG_GREP("/^[A-Z]{1,2}[0-9][0-9A-Z]{0,1}/", `contacts`.`postcode`) = `postcodes`.`postcode` SET `contacts`.`lat` = `postcode`.`lat`, `contacts`.`lng` = `postcode`.`lng`
Is it possible?? Or do I need to use an external script?
Many thanks.
I want to use maxima from python using pexpect, whenever maxima starts it will print a bunch of stuff of this form:
$ maxima
Maxima 5.27.0 http://maxima.sourceforge.net
using Lisp SBCL 1.0.57-1.fc17
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1)
i would like to start up pexpect like so:
import pexpect
cmd = 'maxima'
child = pexpect.spawn(cmd)
child.expect (' match all that stuff up to and including (%i1)')
child.sendline ('integrate(sin(x),x)')
chil.expect( match (%o1 ) )
print child.before
how do i match the starting banner up to the prompt (%i1)?
and so on, also maxima increments the (%i1)'s by one as the session goes along, so the next expect would be:
child.expect ('match (%i2)')
child.sendline ('integrate(sin(x),x)')
chil.expect( match (%o2 ) )
print child.before
how do i match the (incrementing) integers?
I managed to implement a function that converts camel case to words, by using the solution suggested by @ridgerunner in this question:
Split camelCase word into words with php preg_match (Regular Expression)
However, I want to also handle embedded abreviations like this:
'hasABREVIATIONEmbedded' translates to 'Has ABREVIATION Embedded'
I came up with this solution:
<?php
function camelCaseToWords($camelCaseStr)
{
// Convert: "TestASAPTestMore" to "TestASAP TestMore"
$abreviationsPattern = '/' . // Match position between UPPERCASE "words"
'(?<=[A-Z])' . // Position is after group of uppercase,
'(?=[A-Z][a-z])' . // and before group of lowercase letters, except the last upper case letter in the group.
'/x';
$arr = preg_split($abreviationsPattern, $camelCaseStr);
$str = implode(' ', $arr);
// Convert "TestASAP TestMore" to "Test ASAP Test More"
$camelCasePattern = '/' . // Match position between camelCase "words".
'(?<=[a-z])' . // Position is after a lowercase,
'(?=[A-Z])' . // and before an uppercase letter.
'/x';
$arr = preg_split($camelCasePattern, $str);
$str = implode(' ', $arr);
$str = ucfirst(trim($str));
return $str;
}
$inputs = array(
'oneTwoThreeFour',
'StartsWithCap',
'hasConsecutiveCAPS',
'ALLCAPS',
'ALL_CAPS_AND_UNDERSCORES',
'hasABREVIATIONEmbedded',
);
echo "INPUT";
foreach($inputs as $val) {
echo "'" . $val . "' translates to '" . camelCaseToWords($val). "'\n";
}
The output is:
INPUT'oneTwoThreeFour' translates to 'One Two Three Four'
'StartsWithCap' translates to 'Starts With Cap'
'hasConsecutiveCAPS' translates to 'Has Consecutive CAPS'
'ALLCAPS' translates to 'ALLCAPS'
'ALL_CAPS_AND_UNDERSCORES' translates to 'ALL_CAPS_AND_UNDERSCORES'
'hasABREVIATIONEmbedded' translates to 'Has ABREVIATION Embedded'
It works as intended.
My question is:
Can I combine the 2 regular expressions $abreviationsPattern and camelCasePattern
so i can avoid running the preg_split() function twice?
How can I write regular expression in C# to validate that the input does not contain double spaces? I am using Regular Expression Validation. However I do not know what is the Validation Expression to get the result.
"white snake" : success
"white snake" : fail
How can I 301 redirect any URL that starts with a number between 1 - 9999, for example
domain.com/12/something/anotherthing
domain.com/378/product/widgets
domain.com/2560
I have text that looks like:
My name is (Richard) and I cannot do
[whatever (Jack) can't do] and
(Robert) is the same way [unlike
(Betty)] thanks (Jill)
The goal is to search using a regular expression to find all parenthesized names that occur anywhere in the text BUT in-between any brackets.
So in the text above, the result I am looking for is:
Richard
Robert
Jill
I have a partially converted XML document in soup coming from HTML. After some replacement and editing in the soup, the body is essentially -
<Text...></Text> # This replaces <a href..> tags but automatically creates the </Text>
<p class=norm ...</p>
<p class=norm ...</p>
<Text...></Text>
<p class=norm ...</p> and so forth.
I need to "move" the <p> tags to be children to <Text> or know how to suppress the </Text>. I want -
<Text...>
<p class=norm ...</p>
<p class=norm ...</p>
</Text>
<Text...>
<p class=norm ...</p>
</Text>
I've tried using item.insert and item.append but I'm thinking there must be a more elegant solution.
for item in soup.findAll(['p','span']):
if item.name == 'span' and item.has_key('class') and item['class'] == 'section':
xBCV = short_2_long(item._getAttrMap().get('value',''))
if currentnode:
pass
currentnode = Tag(soup,'Text', attrs=[('TypeOf', 'Section'),... ])
item.replaceWith(currentnode) # works but creates end tag
elif item.name == 'p' and item.has_key('class') and item['class'] == 'norm':
childcdatanode = None
for ahref in item.findAll('a'):
if childcdatanode:
pass
newlink = filter_hrefs(str(ahref))
childcdatanode = Tag(soup, newlink)
ahref.replaceWith(childcdatanode)
Thanks
I have a regular expression for phone numbers as follows:
^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$
I have a mask on the phone number textbox in the following format: (___)___-____
How can I modify the regular expression so that it accommodates the mask?
Suppose I have URLs with query string parameters like these:
/index.php?book=DesignPatterns&page=151
/index.php?book=Refactoring&page=157
Using mod_rewrite, how can I redirect them to SES URLs like these?
/DesignPatterns/151
/Refactoring/157
What is the most concise way to transform a string in the following format:
mysql:[/[/]][user[:pass]@]host[:port]/db[/]
Into a usuable PDO connection/instance (using the PDO_MYSQL DSN), some possible examples:
$conn = new PDO('mysql:host=host;dbname=db');
$conn = new PDO('mysql:host=host;port=3307;dbname=db');
$conn = new PDO('mysql:host=host;port=3307;dbname=db', 'user');
$conn = new PDO('mysql:host=host;port=3307;dbname=db', 'user', 'pass');
I've been trying some regular expressions (preg_[match|split|replace]) but they either don't work or are too complex, my gut tells me this is not the way to go but nothing else comes to my mind.
Any suggestions?
I am sending mails (in asp.net ,c#), having a template in text file (.txt) like below
User Name :<User Name>
Address : <Address>.
I used to replace the words within the angle brackets in the text file using the below code
StreamReader sr;
sr = File.OpenText(HttpContext.Current.Server.MapPath(txt));
copy = sr.ReadToEnd();
sr.Close(); //close the reader
copy = copy.Replace(word.ToUpper(),"#" + word.ToUpper()); //remove the word specified UC
//save new copy into existing text file
FileInfo newText = new FileInfo(HttpContext.Current.Server.MapPath(txt));
StreamWriter newCopy = newText.CreateText();
newCopy.WriteLine(copy);
newCopy.Write(newCopy.NewLine);
newCopy.Close();
Now I have a new problem,
the user will be adding new words within an angle, say for eg, they will be adding <Salary>.
In that case i have to read out and find the word <Salary>.
In other words, I have to find all the words, that are located with the angle brackets (<).
How do I do that?
I would like to convert any instances of a hashtag in a String into a linked URL:
#hashtag - should have "#hashtag" linked.
This is a #hashtag - should have "#hashtag" linked.
This is a [url=http://www.mysite.com/#name]named anchor[/url] - should not be linked.
This isn't a pretty way to use quotes - should not be linked.
Here is my current code:
String.prototype.parseHashtag = function() {
return this.replace(/[^&][#]+[A-Za-z0-9-_]+(?!])/, function(t) {
var tag = t.replace("#","")
return t.link("http://www.mysite.com/tag/"+tag);
});
};
Currently, this appears to fix escaped characters (by excluding matches with the amperstand), handles named anchors, but it doesn't link the #hashtag if it's the first thing in the message, and it seems to grab include the 1-2 characters prior to the "#" in the link.
Halp!
I have tried to remove the following tag generated by the AJAX Control toolkit.
The scenario is our GUI team used the AJAX control toolkit to make the GUI but I need to move them to normal ASP .NET view tag using MultiView.
I want to remove all the __designer: attributes
Here is the code
<asp:TextBox ID="a" runat="server" __designer:wfdid="w540" />
<asp:DropdownList ID="a" runat="server" __designer:wfdid="w541" />
.....
<asp:DropdownList ID="a" runat="server" __designer:wfdid="w786" />
I tried to use the regular expression find replace in Visual Studio using:
Find:
:__designer\:wfdid="w{([0-9]+)}"
Replace with empty space
Can any regular expression expert help?
Hi,
Im looking for function (PHP will be the best), which returns true whether exists string matches both regexpA and regexpB.
Example 1:
$regexpA = '[0-9]+';
$regexpB = '[0-9]{2,3}';
hasRegularsIntersection($regexpA,$regexpB) returns TRUE because '12' matches both regexps
Example 2:
$regexpA = '[0-9]+';
$regexpB = '[a-z]+';
hasRegularsIntersection($regexpA,$regexpB) returns FALSE because numbers never matches literals.
Thanks for any suggestions how to solve this.
Henry
There's a few "how do I invert a regexp" questions here on stackoverflow, but I can't find one for vim (if it does exist, by goggle-fu is lacking today).
In essence I want to match all non-printable characters and delete them. I could write a short script, or drop to a shell and use tr or something similar to delete, but a vim solution would be dandy :-)
Vim has the atom \p to match printable characters, however trying to do this :s/[^\p]//g to match the inverse failed and just left me with every 'p' in the file. I've seen the (?!xxx) sequence in other questions, and vim seems to not recognise this sequence. I've not found seen an atom for non-printable chars.
In the interim, I'm going to drop to external tools, but if anyone's got any trick up their sleeve to do this, it'd be welcome :-)
Ta!
I'm new to R and unable to find other threads with a similar issue.
I'm cleaning data that requires punctuation at the end of each line. I am unable to add, say, a period without overwriting the final character of the line preceding the carriage return + line feed.
Sample code:
Data1 <- "%trn: dads sheep\r\n*MOT: hunn.\r\n%trn: yes.\r\n*MOT: ana mu\r\n%trn: where is it?"
Data2 <- gsub("[^[:punct:]]\r\n\\*", ".\r\n\\*", Data1)
The contents of Data2:
[1] "%trn: dads shee.\r\n*MOT: hunn.\r\n%trn: yes.\r\n*MOT: ana mu\r\n%trn: where is it?"
Notice the "p" of sheep was overwritten with the period. Any thoughts on how I could avoid this?
Dear Masters! Is it possible to ensure, that only characters with codes between 0 and 255 will be accepted by regular expression, but all with the codes up to 256 not? Thank you!
How can I convert some regular language to its equivalent Context Free Grammar(CFG)?
Whether the DFA corresponding to that regular expression is required to be constructed or is there some rule for the above conversion?
For example, considering the following regular expression
01+10(11)*
How can I describe the grammar corresponding to the above RE?
I'm trying to find all the occurrences of "Arrows" in text, so in
"<----=====><==->>"
the arrows are:
"<----", "=====>", "<==", "->", ">"
This works:
String[] patterns = {"<=*", "<-*", "=*>", "-*>"};
for (String p : patterns) {
Matcher A = Pattern.compile(p).matcher(s);
while (A.find()) {
System.out.println(A.group());
}
}
but this doesn't:
String p = "<=*|<-*|=*>|-*>";
Matcher A = Pattern.compile(p).matcher(s);
while (A.find()) {
System.out.println(A.group());
}
No idea why. It often reports "<" instead of "<====" or similar.
What is wrong?
Does anyone have suggestions for detecting url's in a set of elements and converting them to links?
$$('#pad dl dd').each(function(s){
//detect urls and convert to a elements.
});
Hi
I am trying to import this:
http://en.wikipedia.org/wiki/List_of_countries_by_continent_%28data_file%29
which is of the format like:
AS AF AFG 004 Afghanistan, Islamic Republic of
EU AX ALA 248 Åland Islands
EU AL ALB 008 Albania, Republic of
AF DZ DZA 012 Algeria, People's Democratic Republic of
OC AS ASM 016 American Samoa
EU AD AND 020 Andorra, Principality of
AF AO AGO 024 Angola, Republic of
NA AI AIA 660 Anguilla
if i do
<? explode(" ",$data"); ?>
that works fine apart from countries with more than 1 word.
how can i split it so i get the first 4 bits of data (the chars/ints) and the 5th bit of data being whatever remains?
this is in php
thank you