user1592380
user1592380

Reputation: 36247

Basic regex capture

I am trying to use regex to capture a string between 2 strings.I'm not experienced with regex. I want to capture the town name in the following string:

On  April  6,  2016,  the  Town  of  Woodstock  auctioned 
±
14.00

I've tried the most basic capture attempt:

town of (.*) auctioned

that I can think of , but I'm not getting any match at all. A link is below. what am I doing wrong?

see here

Upvotes: 0

Views: 53

Answers (2)

Mustofa Rizwan
Mustofa Rizwan

Reputation: 10466

If you say that the regex would start with town of and end with auctioned

then:

/town\s+of\s+(.*?)\s+auctioned/ig

here /i makes it case insensitive in javascript. here capture group 1 contains your town name

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336158

First, regexes are case-sensitive by default. Use the (?i) inline modifier to change that (assuming a regex engine that doesn't expect its modifiers after the regex).

Second, whitespace is treated like any other character. If the text contains two spaces between words, then your regex won't match if it uses only one space.

Lastly, you probably should use a lazy quantifier:

(?i)town\s+of\s+(.*?)\s+auctioned

should work.

Upvotes: 4

Related Questions