regex

Delete all characters in a multline string up to a given pattern

Using Python I need to delete all charaters in a multiline string up to the first occurrence of a given pattern. In Perl this can be done using regular expressions with something like: #remove all chars up to first occurrence of cat or dog or rat $pattern = 'cat|dog|rat' $pagetext =~ s/(.*?)($pattern)/$2/xms; What's the best way to ...

How to Regex.IsMatch at a specified offset in .NET?

Suppose I want to match "abc" within the string s only if it occurs exactly at index n. int n = 2; Console.WriteLine(new Regex("abc").IsMatch("01abc", n)); // true Console.WriteLine(new Regex("abc").IsMatch("0123abc", n)); // true (but want false) Console.WriteLine(new Regex("^abc").IsMatch("01abc", n)); // false (but want true) Seems...

Getting the title of a page in PHP

Hi. When I want to get the title of a remote webiste, I use this script: function get_remotetitle($urlpage) { $file = @fopen(($urlpage),"r"); $text = fread($file,16384); if (preg_match('/<title>(.*?)<\/title>/is',$text,$found)) { $title = $found[1]; } else { $title = 'Title N/A'; } return $title; ...

Regexp for extracting data in parenthesis and commas

So, i have this : "( ABC,2004 )" And I would need to extract ABC in a variable and 2004 in another. So what I have for now is this: In: re.compile(r'([^)]*,').findall("( ABC,2004 )") Out: ['( ABC,'] ...

Using regex to add leading zeroes

I would like to add a certain number of leading zeroes (say up to 3) to all numbers of a string. For example: Input: /2009/5/song 01 of 12 Output: /2009/0005/song 0001 of 0012 What's the best way to do this with regular expressions? Edit: I picked the first correct answer. However, all answers are worth giving a read. ...

Why does my regex fail when the number ends in 0?

This is a really basic regex question but since I can't seem to figure out why the match is failing in certain circumstances I figured I'd post it to see if anyone else can point out what I'm missing. I'm trying to pull out the 2 sets of digits from strings of the form: 12309123098_102938120938120938 1321312_103810312032123 123123123_1...

Little Regular Expression (against HTML) help

Hi, I have the following HTML <p>Some text <a title="link" href="http://link.com/" target="_blank">my link</a> more text <a title="link" href="http://link.com/" target="_blank">more link</a>.</p> <p>Another paragraph.</p> <p>[code:cf]</p> <p>&lt;cfset ArrFruits = ["Orange", "Apple", "Peach", "Blueberry", </p> <p>"Blackberry", "Strawber...

RegEx for matching alternating case letters

I would like to detect the following sequences: a aA aAa aAaA ... where a~[a-z] and A~[A-Z], the case alternates and the first letter is always lower-case. Thanks, Tom ...

Java split regular expression

Hi all, If I have a string, e.g. setting=value How can I remove the '=' and turn that into two separate strings containing 'setting' and 'value' respectively? Thanks very much! ...

Java regex to get part number

I have HTML that I need to extract a part number from, the HTML looks like: javascript:selectItem('ABC123 1', '..... I need to get the ABC123 from the above. My code snippet: Patterp p = Pattern.Compile("?????"); Matcher m = p.matcher(html); if(m.find()) partNumber = m.group(1).trim(); BTW, in the pattern, how do I esc...

Can I safely use extended regular expressions all the time rather than basic?

It appears that most modern languages and tools allow for extended regular expressions, and ERE looks a lot cleaner than BRE with all those backslashes. Are there any major drawbacks in compatibility or maintainability when using ERE instead of BRE? ...

Java regex, need help with escape characters

My HTML looks like: <td class="price" valign="top"><font color= "blue">&nbsp;&nbsp;$&nbsp; 5.93&nbsp;</font></td> I tried: String result = ""; Pattern p = Pattern.compile("\"blue\">&nbsp;&nbsp;$&nbsp;(.*)&nbsp;</font></td>"); Matcher m = p.matcher(text); if(m.find()) result = m.group(1).tri...

Will this Java regex return a single result or multiple

If my HTML is: <tr><td>....</td><hr></tr> <tr><td>....</td><hr></tr> <tr><td>....</td><hr></tr> <tr><td>....</td><hr></tr> <tr><td>....</td><hr></tr> <tr><td>....</td><hr></tr> If my regex is: Patterp p = Pattern.compile("<tr>(.*)<hr></tr>"); Should this get 1 result or all the individual rows? Is there a way to force it to get al...

How to parse bbcodes safely?

I'm trying to parse BBcodes in php but i don't think my code is safe at all. $Text = preg_replace("(\[color=(.+?)\](.+?)\[\/color\])is","<span style=\"color: $1\">$2</span>",$Text); I think you can pass an injection like this and it will work: [color=<script>alert('gotcha');</script>]...[/color] How to improve my regex to only cap...

php preg_match pattern problem,regular expression pattern

<tr id='ieconn3' > <td><table width='100%'><tr><td valign='top'><table width='100%'><tr><td>aaaaa <br>&nbsp;</td></tr><tr><td> I want to get the aaaaa part till <br> or </td>. I tried lots of patterns but didnt work. any help? ...

Java - Regex problem

I have a list of URLs of type http://www.example.com/pk/ca, http://www.example.com/pk, http://www.example.com/anthingcangoeshere/pk, and http://www.example.com/pkisnotnecessaryhere. Now, I want to find out only those URLs that ends with /pk or /pk/ and don't have anything in between .com and /pk ...

Java - Regex problem

Possible Duplicate: Java - Regex problem I have list of URLs of types: http://www.example.com/pk/etc http://www.example.com/pk/etc/ http://www.example.com/pk/etc/etc where etc can be anything. So I want to search only those URLs that contains www.example.com/pk/etc or www.example.com/pk/etc/. Note: It is for all those...

Simple PHP Regex question

Hi all, I'd like to validate a field in a form to make sure it contains the proper formatting for a URL linking to a Vimeo video. Below is what I have in Javascript, but I need to convert this over to PHP (not my forte) Basically, I need to check the field and if it is incorrectly formatted, I need to store an error message as a variab...

need a regex for matching repeating lines of symbols (example: ------------- or *****************)

I want to be able to remove linebreaks etc that people make by using recurring characters, for example: **************************************************** ---------------------------------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ etc i'd like to not have to specify which characters it will match, maybe all that are NOT \w ...

initial caps in actionScript using Regex

I'm trying to do initial caps in actionScript with no loops but now i'm stuck. I wanted to select the first letter or every word then apply uppercase on that letter. Well I got the selection part right, but at a dead end right now, any ideas? I was trying to do this with out loops and cutting up strings. //replaces with x cant figure o...