regex

Regex to capture an optional group in the middle of a block of input

I'm stuck on a RegEx problem that's seemingly very simple and yet I can't get it working. Suppose I have input like this: Some text %interestingbit% lots of random text lots and lots more %anotherinterestingbit% Some text %interestingbit% lots of random text OPTIONAL_THING lots and lots more %anotherinterestingbit% Some text %interesti...

Regular expression to parse an array of JSON objects?

I'm trying to parse an array of JSON objects into an array of strings in C#. I can extract the array from the JSON object, but I can't split the array string into an array of individual objects. What I have is this test string: string json = "{items:[{id:0,name:\"Lorem Ipsum\"},{id:1,name" + ":\"Lorem Ipsum\"},{id:2,name:\...

Can Regex/preg_replace be used to add an anchor tag to a keyword?

I would like to be able to switch this... My sample [a id="keyword" href="someURLkeyword"] test keyword test[/a] link this keyword here. To... My sample [a id="keyword" href="someURLkeyword"] test keyword test[/a] link this [a href="url"]keyword[/a] here. I can't simply replace all instances of "keyword" because some are used in or w...

RewriteRule but keep the subdomain

I would like to redirect the following as an example: A.olddomain.com.au/blah.html > A.newdomain.com/blah.html B.olddomain.com.au/blah.html > B.newdomain.com/blah.html Essentially, I have a variable number of subdomains and I only want to change the domain name itself on the redirect. Any clues or suggestions to try out? Thanks....

RegularExpression To Validate A Textbox That Should Allow Only Decimals And Numbers

Hi Guys, I have a textbox for 'Area '.I need a regularexpression to validate textbox such that it should allow decimals to enter but no characters.Anyone can help me ...

Regex to get string between curly braces "{I want what's between the curly braces}"

Hi, unfortunately despite having tried to learn regex at least one time a year for as many years as I can remember, I always forget as I use them so infrequently.This year my new years resolution is to not try and learn regex again - So this year to save me from tears I'll give it to stackoverflow. (Last Christmas remix). So basically I...

using static Regex.IsMatch vs creating an instance of Regex

In C# should you have code like: public static string importantRegex = "magic!"; public void F1(){ //code if(Regex.IsMatch(importantRegex)){ //codez in here. } //more code } public void main(){ F1(); /* some stuff happens...... */ F1(); } or should you persist an instance of a Regex containing the important pattern?...

How can use RegEx to match a list of strings in C#?

I need to find all the regex matches from a list of strings. For example, I need to be able to take the string "This foo is a foobar" and match any instances of either "foo" or "bar". What would the correct pattern be for this? Also, what input sanitation would I need to do to prevent the inputted text from breaking the pattern? ...

How can I profile Perl regexes?

What's the best way to profile Perl regexes to determine how expensive they are? ...

When should I use a compiled Regex vs. interpreted?

After reading this article http://www.codinghorror.com/blog/archives/000228.html I understand the benefits of compiled regular expressions a little better, however in what personal scenarios would you consider mandates the use of a compiled Reg Ex? For instance, I am using a regex in a loop and the regular expression string utilises dif...

Regex Named Groups in Java

It is my understanding that the java.regex package does not have support for named groups (http://www.regular-expressions.info/named.html) so can anyone point me towards a third-party library that does? I've looked at jregex but its last release was in 2002 and it didn't work for me (admittedly I only tried briefly) under java5. ...

Are explicitly typed regexes allowed as keys in Perl YAML dump?

This relates to a previous question: How can I read Perl data structures from Python?. It could be a bug in the version of the YAML parser that I'm working with (0.66), but when I run: perl -MYAML -le 'do shift; print YAML::Dump( $CPAN::Config )' simple.pl On the following simple.pl: %config = ( 'color' => 'red', 'numbers' =>...

Check if string is a prefix of a Javascript RegExp

In Javascript I have defined a regular expression and now a user is typing in a string. I want to tell him if his string still could match the RegExp if he continues typing or if he's already on the wrong way. For instance: var re = /a*b/; "a".isPrefixOf( re ); // true "x".isPrefixOf( re ); // false How could an implementation of isP...

regular expression "contains" another regular expression

Is there a way to test if a regular expression "contains" another regular expression? For example: RegEX1 = "a.*b"; RegEx2 = "a1.*b"; RegEX1 "contains" RegEX2. As far as I know - this can't be done, am I wrong? OK, joel.neely has shown that it can be done (haven't read it yet...) academically. Can it be done in a programming language...

Regex to validate regex [closed]

Duplicate of http://stackoverflow.com/questions/362793/regexp-that-matches-valid-regexps How do you produce a regex that matches only valid regex? For instance: "[hc]at" would be valid (matching "hat" and "cat"), but "[hcat" would be invalid, as it is missing ]. ...

how to parse string for capitalized words

Hi, I have this string: " Mimi loves Toto and Tata hate Mimi so Toto killed Tata" I want to write a code that print only the words that begin with capital letters, avoiding repetition the Output should be like Mimi Toto Tata I tried to do so but I'm sure its wrong even though no errors are showing. The code i wrote : static void...

regex replace method

class Program { static void Main(string[] args) { string s = "the U.S.A love UK"; Console.WriteLine(replace(s)); } public static string replace(string s) { s = Regex.Replace(s, @"^U.S.A", " United state Of America"); s = Regex.Replace(s, @"^Uk", ...

Capture string until first caret sign hit in regex?

I am working with legacy systems at the moment, and a lot of work involves breaking up delimited strings and testing against certain rules. With this string, how could I return "Active" in a back reference and search terms, stopping when it hits the first caret (^)?: Active^20080505^900^LT^100 Can it be done with an inclusion in the ...

How can I substitute regexp matches and map the substitutions in Perl?

I.e.: echo H#97llo | MagicPerlCommand Stdout: Hallo were MagicPerlCommand is something like perl -pnle "s/#(\d+)/chr(\1)/ge" (but that doesn't work). ...

Help with regex in javascript

I have this sample text, which is retrieved from the class name on an html element: rich-message err-test1 erroractive rich-message err-test2 erroractive rich-message erroractive err-test1 err-test2 rich-message erroractive I am trying to match the "test1"/"test2" data in each of these examples. I am currently using the following rege...