regex

How to determine if a number is a prime with regex?

I found the following code example for Java on RosettaCode: public static boolean prime(int n) { return !new String(new char[n]).matches(".?|(..+?)\\1+"); } I don't know Java in particular but understand all aspects of this snippet except for the regex itself I have basic to basic-advanced knowledge of Regex as you find it in the b...

Inconsistent behavior with RegExp Object in RegExp.match

I have detect a strange behavior in regexps created with the RegExp object: With this code: var exp1 = /./; var exp2 = new RegExp('.'); ​ var test1 = exp1.test('large\n\ntext..etc.'); var test2 = exp2.test('large\n\ntext..etc.'); ​ var match1 = 'large\n\ntext..etc.'.match(exp1); var match2 = 'large\n\ntext..etc.'.match(exp2); ...the ...

How can i do this using a Python Regex?

I am trying to properly extract methods definitions that are generated by comtypes for Com Interfaces using a regex. Furthermore some of them are blank which causes even more problems for me. Basically i have this: IXMLSerializerAlt._methods_ = [ COMMETHOD([helpstring(u'Loads an object from an XML string.')], HRESULT, 'LoadFromStri...

Regexp to extract sources from different video embeds

My SMF forum contains posts with video and I want to extract them to show on the Wordpress main page. My current regexp (thanks to SO!) extracts the url of the videos, which I embed using AutoEmbed. Everything works up until a post looks like this: <embed height="600" width="600" allowscriptaccess="never" quality="high" loop="true" pl...

JavaScript: Given an offset and substring length in an HTML string, what is the parent node?

My current project requires locating an array of strings within an element's text content, then wrapping those matching strings in <a> elements using JavaScript (requirements simplified here for clarity). I need to avoid jQuery if at all possible - at least including the full library. For example, given this block of HTML: <div> <p>T...

Replace symbol "%" with word "Percent"

Hi, How to replace symbol "%" with a word "Percent". My original string is "Internal (%) External (%)". The string should be "Internal (Percent) External (Percent)" Using regular expression, how I can replace this symbol? Thanks in advance. Atul ...

regex removing specified empty xml tag using C#

Hi, I would like to remove tag like the following one with its attributes using C# .Net how can i do it? <aaa type="1" class="2" /> other tags like <bbb type="5" class="4" /> i would like to keep. Best Regards, ...

Regular Expression to break row with comma separated values into distinct rows

I have a file with many rows. Each row has a column which may contain comma separated values. I need each row to be distinct (ie no comma separated values). Here is an example row: AB AB10,AB11,AB12,AB15,AB16,AB21,AB22,AB23,AB24,AB25,AB99 ABERDEEN Aberdeenshire The columns are comma separated (Postcode area, Postcode districts,...

Regex : Matching phone numbers starting with NNN and having 10 numbers

I need to match phone numbers that : start with 010 or 012 or 016 or 019 Consist of 10 numbers exactly can you please help me on how to match numbers using PHP and regex thanks ...

Url for SEO Link

Hi, i need a function (c#) or regular expression that makes me a nice URL out of a string. (and replaces invalid characters) Something like here on stackoverflow.. example: Short URL or long URL for SEO -> short-url-or-long-url-for-seo Thanks ...

php - regex - catch string inside multiple tags

Hi all guys! still on regex! ;-))) Assuming we have an html file with a lot of <tr> rows with same structure like this below, where (.*?) is the content i need to extract! <tr align=center><th width=5%><a OnClick="(.*?)"href=#>(.*?)</a><td width=5%>(.*?)<td width=5% align=center >(.*?)</td></tr> UPDATED maybe with a nice preg_matc...

Find a regular expression that matches a string?

I need a model for finding all of the regular expressions that would match a particular string. Basically, I need an algorithm for doing what I do to generate a regex search string from some pattern. My purpose for this to create a list of potential regular expressions from a selection of text and order that list from least specific (i...

Difference in regex between Python and Rubular?

In Rubular, I have created a regular expression: (Prerequisite|Recommended): (\w|-| )* It matches the bolded: Recommended: good comfort level with computers and some of the arts. Summer. 2 credits. Prerequisite: pre-freshman standing or permission of instructor. Credit may not be applied toward engineering degree. S-U ...

Find and Replace a section of a string with wildcard type search using RegEx (while retaining some text)

I am trying to replace some text in a string with different text while retaining some of the text and I am having trouble doing it. My code is: StreamReader reader = new StreamReader(fDialog.FileName.ToString()); string content = reader.ReadToEnd(); reader.Close(); /Replace M2 with M3 (this works fine) content = Regex.Replace(conten...

Perl equivalent of PHP's preg_callback

Do we have a preg_callback equivalent in Perl ? Lets say I want to match something and replace it with the return value of the function that is called with the matched thing. ...

How do I replace an actual asterisk character (*) in a Regex expression?

I have a statement: I have a string such as content = "* test *" I want to search and replace it with so when I am done the string contains this: content = "(*) test (*)" My code is: content = Regex.Replace(content, "*", "(*)"); But this causes an error in C# because it thinks that the * is part of the Regular Express...

Wanted: a very simple Java RegExp API

I'm tired of writing Pattern p = Pattern.compile(... Matcher m = p.matcher(str); if (m.find()) { ... Over and over again in my code. I was going to write a helper class to make it neater, but I then I wondered: is there a library that tries to provide a simpler facade for Regular Expressions in Java? I'm thinking something in the...

Replacing strings with regex in JavaScript

A particular regular expression is bugging me right now. I simply want to replace the range=100 in a string like var string = '...commonstringblabla<b>&range=100&</b>stringandsoon...'; with ...commonstringblabla<b>&range=400&</b>stringandsoon... I successfully matched the "range=100"-part with alert( string.match(/range=100/) ...

Problem with regular expression for some special parttern.

Hi all, I got a problem when I tried to find some characters with following code: $str = "统计类型目前分为0日Q统计,月统q计及287年7统1计三7种,如需63自定义时间段,点1击此hell处进入自o定w义统or计d!页面。其他统计:客服工作量统计 | 本周服务统计EXCEL"; preg_match_all('/[\w\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A]/',$str,$match); //line 5 print_r($match); And I got error as below: Warning: preg_m...

Find last match with python regular expression

I wanto to match the last occurence of a simple pattern in a string, e.g. list = re.findall(r"\w+ AAAA \w+", "foo bar AAAA foo2 AAAA bar2) print "last match: ", list[len(list)-1] however, if the string is very long, a huge list of matches is generated. Is there a more direct way to match the second occurence of "AAAA" or should I use ...