regex

Regex to validate URIs

How do you produce a regex that matches only valid URI. The description for URIs can be found here: http://en.wikipedia.org/wiki/URI_scheme. It doesn't need to extract any parts, just test if a URI is valid. (preferred format is .Net RegularExpression) (.Net Version 1.1) Doesn't neet to check for a known protocol, just a valid one. ...

Regex (C#): Replace \n with \r\n

How can I replace lone instances of \n with \r\n (LF alone with CRLF) using a regular expression in C#? Sorry if it's a stupid question, I'm new to Regex. I know to do it by: myStr.Replace("\n", "\r\n"); myStr.Replace("\r\r\n", "\r\n"); But this is inelegant, and would destroy any "\r+\r\n" already in the text (not that it's likely ...

Quick way to find a value in HTML (Java)

Using regex, how is the simplest way to fetch a websites HTML and find the value inside this tag (or any attribute's value for that matter): <html> <head> [snip] <meta name="generator" value="thevalue i'm looking for" /> [snip] ...

Best way to fetch an "unstandard" HTML tag

I'm trying to fetch some HTML from various blogs and I've noticed that different providers use the same tag in different ways. For example, here are two major providers that use the Generator differently: Blogger: <meta content='blogger' name='generator'/> (content first, name later and, yes, single quotes!) Wordpress: <meta name="gene...

Is regex case insensitivity slower?

Source RegexOptions.IgnoreCase is more expensive than I would have thought (eg, should be barely measurable) Assuming that this applies to PHP, Python, Perl, Ruby etc as well as C# (which is what I assume Jeff was using), how much of a slowdown is it and will I incur a similar penalty with "/[a-zA-z]/" as I will with "/[a-z]/i" ? ...

Regex Testing Tools

Hi all, I know of The Regulator for testing regular expressions. And there's also RegExr for testing regular expressions as well. Does anyone know of other regex testing tools? Do any of these tools allow you to specify which RE engine you're testing against? I changed this question to community wiki ...

Validate a UK phone number

How do I validate a UK phone number in C# using a regex? ...

Easiest way to convert a URL to a hyperlink in a C# string?

I am consuming the Twitter API and want to convert all URLs to hyperlinks. What is the most effective way you've come up with to do this? from string myString = "This is my tweet check it out http://tinyurl.com/blah"; to This is my tweet check it out <a href="http://tinyurl.com/blah"&gt;http://tinyurl.com/&gt;blah&lt;/a&gt; ...

Looking for Regex to find quoted newlines in a big string (for C#)

I have a big string (let's call it a CSV file, though it isn't actually one, it'll just be easier for now) that I have to parse in C# code. The first step of the parsing process splits the file into individual lines by just using a StreamReader object and calling ReadLine until it's through the file. However, any given line might contai...

How would you abbriviate XHTML to an arbitrary number of words?

Hello, How would you programmacially abbriviate XHTML to an arbitrary number of words without leaving unclosed or corrupted tags? E.g. this: <p> Proin tristique dapibus neque. Nam eget purus sit amet leo tincidunt accumsan. </p> <p> Proin semper, orci at mattis blandit, augue justo blandit nulla. <span>Quisque ante con...

Regex to replace Boolean with bool

I am working on a C++ code base that was recently moved from X/Motif to Qt. I am trying to write a Perl script that will replace all occurrences of Boolean (from X) with bool. The script just does a simple replacement. s/\bBoolean\b/bool/g There are a few conditions. 1) We have CORBA in our code and \b matches CORBA::Boolean w...

Replacing the nth instance of a regex match in Javascript

I'm trying to write a regex function that will identify and replace a single instance of a match within a string without affecting the other instances. For example, I have this string: 12||34||56 I want to replace the second set of pipes with ampersands to get this string: 12||34&&56 The regex function needs to be able to handle x...

Test/expand my email regex

I'm really not confident with Regex, I know some basic syntax but not enough to keep me happy. I'm trying to build a regular expression to check if an email is valid. So far here's what I've got: [A-Za-z0-9._-]+@[A-Za-z0-9]+.[A-Za-z.]+ It needs to take account of periods in the username/domain and I think it works with multiple TLDs ...

How do I find broken NMEA log sentences with grep?

My GPS logger occassionally leaves "unfinished" lines at the end of the log files. I think they're only at the end, but I want to check all lines just in case. A sample complete sentence looks like: $GPRMC,005727.000,A,3751.9418,S,14502.2569,E,0.00,339.17,210808,,,A*76 The line should start with a $ sign, and end with an * and a tw...

replace URL with HTML Links javascript

i am using the next function to match urls inside a given text and replace them for html links. The regex is working great but currently i am only replacing the first match. Does anyone knows how I can replace all the url? I guess i should be using exec command but i did not really figured how to do it. function replaceURLWithHTMLLinks...

What is the regex pattern for datetime (2008-09-01 12:35:45 ) ?

Dont have any additional information. Just what is the regex pattern for datetime (2008-09-01 12:35:45 ) ? @Espo : I get this error : No ending delimiter '^' found using: preg_match('(?n:^(?=\d)((?31(?!(.0?[2469]|11))|30(?!.0?2)|29(?(.0?2)(?=.{3,4}(1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(16|[2468][048]|[3579][26])00))|0?[1-9]|...

Need a little REGEX help... [PHP]

Hey so what I want to do is snag the content for the first paragraph. The string $blog_post contains a lot of paragraphs in the format: <p>Paragraph 1</p><p>Paragraph 2</p><p>Paragraph 3</p> The problem I'm running into is that I am writing a regex to grab everything between the first P tag and the first closing P tag, however it is ...

Regex to match unique substrings

Here's a basic regex technique that I've never managed to remember. Let's say I'm using a fairly generic regex implementation (e.g., grep or grep -E). If I were to do a list of files and match any that end in either ".sty" or ".cls", how would I do that? ...

Regular expression to match (C) function calls

Does anyone have a regular expression for matching function calls in C programs ? ...

Parsing a log file with regular expressions

I'm currently working on a parser for our internal log files (generated by log4php, log4net and log4j). So far I have a nice regular expression to parse the logs, except for one annoying bit: Some log messages span multiple lines, which I can't get to match properly. The regex I have now is this: (?<date>\d{2}/\d{2}/\d{2})\s(?<time>\d{2...