capturing-group

Regex with optional part doesn't create backreference

I want to match an optional tag at the end of a line of text. Example input text: The quick brown fox jumps over the lazy dog {tag} I want to match the part in curly-braces and create a back-reference to it. My regex looks like this: ^.*(\{\w+\})? (somewhat simplified, I'm also matching parts before the tag): It matches the line...

Saving substrings using Regular Expressions

I'm new to regular expressions in Java (or any language, for that matter) and I'm wanting to do a find using them. The tricky part that I don't understand how to do is replace something inside the string that matches. For example, if the line I'm looking for is Person item6 [can {item thing [wrap]}] I'm able to write a regex that fin...

Is there a regex flavor that allows me to count the number of repetitions matched by the * and + operators?

Is there a regex flavor that allows me to count the number of repetitions matched by the * and + operators? I'd specifically like to know if it's possible under the .NET Platform. ...

Scala regex Named Capturing Groups

In scala.util.matching.Regex trait MatchData I see that there support for groupnames , I thought that this was related to (Regex Named Capturing Groups) But since Java does not support groupnames until version 7 as I understand it (ref), Scala version 2.8.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6. gives me this exception: scala>...

Scala capture group using regex

Let's say I have this code: val string = "one493two483three" val pattern = """two(\d+)three""".r pattern.findAllIn(string).foreach(println) I expected findAllIn to only return 483, but instead, it returned two483three. I know I could use unapply to extract only that part, but I'd have to have a pattern for the entire string, something...

Capturing <thisPartOnly> and (thisPartOnly) with the same group

Let's say we have the following input: <amy> (bob) <carol) (dean> We also have the following regex: <(\w+)>|\((\w+)\) Now we get two matches (as seen on rubular.com): <amy> is a match, \1 captures amy, \2 fails (bob) is a match, \2 captures bob, \1 fails This regex does most of what we want, which are: It matches the open and...

What am I doing wrong with my regex?

Hello, I am trying to capture "Rio Grande Do Leste" from: ... <h1>Rio Grande Do Leste<br /> ... using var myregexp = /<h1>()<br/; var nomeAldeiaDoAtaque = myregexp.exec(document); what am I doing wrong? update: 2 questions remain: 1) searching (document) didn´t produce any result, but changing it to (document.body.innerHTML) w...

How to capture an arbitrary number of groups in JavaScript Regexp?

I would expect this line of JavaScript: "foo bar baz".match(/^(\s*\w+)+$/) to return something like: ["foo bar baz", "foo", " bar", " baz"] but instead it returns only the last captured match: ["foo bar baz", " baz"] Is there a way to get all the captured matches? ...

Regular expression in Yahoo Pipes

Hi, I want to know what regular expression should be applied to replace 1 - 55 of 55 to only get 55 in Regex module of yahoo pipes. Thanks ...

How does this regex find triangular numbers?

Part of a series of educational regex articles, this is a gentle introduction to the concept of nested references. The first few triangular numbers are: 1 = 1 3 = 1 + 2 6 = 1 + 2 + 3 10 = 1 + 2 + 3 + 4 15 = 1 + 2 + 3 + 4 + 5 There are many ways to check if a number is triangular. There's this interesting technique that uses re...

How can we match a^n b^n with Java regex?

This is the second part of a series of educational regex articles. It shows how lookaheads and nested references can be used to match the non-regular languge anbn. Nested references are first introduced in: How does this regex find triangular numbers? One of the archetypal non-regular languages is: L = { anbn: n > 0 } This i...

Regex - Saving Repeating Captured Group

This is what I'm doing a = "%span.rockets#diamonds.ribbons.forever" a = a.match(/(^\%\w+)([\.|\#]\w+)+/) puts a.inspect This is what I get #<MatchData "%span.rockets#diamonds.ribbons.forever" 1:"%span" 2:".forever"> This is what I want #<MatchData "%span.rockets#diamonds.ribbons.forever" 1:"%span" 2:".rockets" 3:".#diamonds" 4:".r...

How do I use regular expression capturing groups with JFlex?

Although this question is about JFlex, it probably applies to other scanner generators such as lex, gnu-flex as well. If I have some rule, how can I create a capturing group in part of that rule and use the result of that captured group as an argument to the code that gets called upon the rule matching? For example, let's say I had a s...