regex

Does Ruby have an addon similar to Perl 6 grammars?

Perl has been one of my go-to programming language tools for years and years. Perl 6 grammars looks like a great language feature. I'd like to know if someone has started something like this for Ruby. ...

Why is 'http://dd ' a valid URL?

I'm writing a .NET 3.5 app and using URI.IsWellFormedUriString(string uriString, UriKind uriKind) to verify user-inputted URIs; using UriKind.Absolute. I was just playing with the application and I'm a bit worried and confused as to why something like: http://ddd is a valid URI? What gives? I know it's because it's part of the RFC, b...

Regex to match specific comments in code

Are there any regex experts who can help me clean up the following source code? I'm going through some existing code and I see several instances similar to the following: public enum Numbers { /// <summary> /// One = 1, /// </summary> One = 1, /// <summary> /// Two = 2, /// </summary> Two = 2, //...

PHP Split a string with start and stop value

I have fooled around with regex but can't seem to get it to work. I have a file called includes/header.php I am converting the file into one big string so that I can pull out a certain portion of the code to paste in the html of my document. $str = file_get_contents('includes/header.php'); From here I am trying to get return only th...

regex matches repeating group {0,2} or {0,4} but {0,3} doesn't.

first, this is using preg. String I'm trying to match: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa b c d xp My regex and their matches: (\S*\s*){0,1}\S*p = "d xp" (\S*\s*){0,2}\S*p = "c d xp" (\S*\s*){0,3}\S*p = NO MATCH (expecting "b c d xp" (\S*\s*){0,4}\S*p = entire string (\S*\s*){0,5}\S*p = entire string Oddly, if I remove a single ...

Rotation in PHP's regex

How can you match the following words by PHP, either by regex/globbing/...? Examples INNO, heppeh, isi, pekkep, dadad, mum My attempt would be to make a regex which has 3 parts: 1st match match [a-zA-Z]* [a-zA-Z]? rotation of the 1st match // Problem here! The part 3 is the problem, since I do not know...

Help with my regular expression. (Python)

If string "x" contains any letter or number, print that string. How to do that using regular expressions? Below is wrong? if re.search('^[A-Z]?[a-z]?[0-9]?', i): print i ...

Select string with comma (regex)

I need an regular expression which matches the following: '"test foo bar", "test"' => no match '"test, foo bar", "test"' => "test, foo bar" '"test, foo bar"' => "test, foo bar" '"test foo bar"' => no match '"test", "test, foo"' => "test, foo" '"test", ","' => no match '"test", 0, 0, "foo"' => no match the regex should match any string...

Is String.replace any faster than String.split ... String.join in ActionScript 3?

Is it any faster to use myString.replace(/foo/g,"bar") rather than myString.split("foo").join("bar") for long strings in ActionScript 3? Or are they just two comparable methods of achieving the same result? ...

Regexp : how to get every group of MatchData ?

I have the following Regexp : regexp=/(\w+) \s* : \s* (\w+) \s+ (.*) \s* ;?/ix And I am trying to get the captures: names, direction, type = iotext.match(regexp).captures This works fine for a single "x : in integer;" , but how could I also get all the groups of other match data in my file : "x : in integer; y : in logic; z : i...

Forward slash in a Python regex

I'm trying to use a Python regex to find a mathematical expression in a string. The problem is that the forward slash seems to do something unexpected. I'd have thought that [\w\d\s+-/*]* would work for finding math expressions, but it finds commas too for some reason. A bit of experimenting reveals that forward slashes are the culprit. ...

RegEx for String.Format

Hiho everyone! :) I have an application, in which the user can insert a string into a textbox, which will be used for a String.Format output later. So the user's input must have a certain format: I would like to replace exactly one placeholder, so the string should be of a form like this: "Text{0}Text". So it has to contain at least on...

running grep from within GNU make

I need to find the text 'ifeq ($(Param1)' using grep. I try to assign search result to make variable. The problem is that single quotes don't escape text in make so when I try: GrepResult:= $(shell grep 'ifeq ($$(Param1)' TextFile) I get: Makefile:214: *** unterminated call to function `shell': missing `)'. Stop. The $ can be esca...

Is there an Alpha matcher for .NET Regex?

The usual alpha symbol for regular expressions \w in the .NET Framework matches alphanumeric symbols, and thus is equivalent to [a-zA-Z0-9], right? There is any [a-zA-Z] equivalent in .NET? ...

REGEX (in PHP).. remove non-alphanumeric characters at ends?

$test = "!!! sdfsdf sd$$$fdf ___"; $test = str_replace(' ', '_', $test); // Turn all spaces into underscores. echo $test."<br />"; // Output: !!!___sdfsdf___sd$$$fdf______ $test = preg_replace('/[^a-zA-Z0-9_-]/', '-', $test); // Replace anything that isn't alphanumeric, or _-, with a hyphen. echo $test."<br />"; // Output: !!!___s...

How do I use Perl to intersperse characters between consecutive matches with a regex substitution?

The following lines of comma-separated values contains several consecutive empty fields: $rawData = "2008-02-06,8:00 AM,14.0,6.0,59,1027,-9999.0,West,6.9,-,N/A,,Clear\n 2008-02-06,9:00 AM,16,6,40,1028,12,WNW,10.4,,,,\n" I want to replace these empty fields with 'N/A' values, which is why I decided to do it via a regex substitution. ...

Why is String#scan not finding all the matches?

Let's use this as sample data : text=<<EOF #if A==20 int b = 20; #else int c = 30; #endif And this code : puts text.scan(/\#.*?\#/m) Why is this only capturing this: #if A==20 int b = 20; # I was expecting this to match as well: #else int c = 30; # What do I have to modify so that it captures that as well?...

Parse string with regex

Hello. I have a string I need to parse. The problem is that some parts of the string is not always the same. a3:S8:[gmpage]S17:Head GM NecrocideS12:test [15158] The first 18 chars are always the same, so those can i String.Substring() out with ease. My problem is that the characters S12: not always is S12:, it could easily be S...

Remove first three occurrences of space

I need to remove the first three occurrences of space per line in a text file. I have tried the following: sed 's/ //3' This only removes the third occurrence. sed 's/ //3g' This leaves the first three occurrences of space alone and removes all of the following, this is the exactly the opposite of what i want. ...

How can i replace all '\' and '/' characters, in a string to '-', in javascript.

Hi folks, I have the following javascript. window.location.href = _searchUrl + query.replace(/\s+/g, '+') .replace('/', '-') .replace('\\','-') this replaces all spaces with + and only the first \ and first / with a -. i need to make it replace ALL \ a...