0

I feel like an idiot because I've spent a long time trying to find a regex that will work.

String: ~05276~^~0500~^~Turkey...

The ... means that there can be an unlimited number of characters after. What I want is the first tilde delimited number without the tildes. I'm trying to pull some data from a text file, and I think that I can figure the rest out if I can understand how to do this.

Here's my regex as it stands: /^~([\d]+)~/

This is what I'm getting:

[ '~05276~',
 '05276',
 index: 0,
 input: '~05276~^~0500~^~Turkey...' ]

When I use the g operator (/^~([\d]+)~/g), I'm only getting the ~05276~, and what I want is the 05726 (no tildes).

I've found a few different posts and resources, but I can't seem to figure out why this isn't working as I expect. Here's what I found:

Javascript regex - how to get text between curly brackets

Is JavaScript/NodeJS capable of this?

Edit:

Here's my code:

lineReader.eachLine(file, function (line) {
    var entry = {};

    entry.id = line.match(/^~([\d]+)~/);

    console.log(entry);
});

lineReader is working properly and returns a line like in my example string above.

Community
  • 1
  • 1
beatgammit
  • 19,817
  • 19
  • 86
  • 129

2 Answers2

2

You'r regex is (almost) fine, but you're probably not using it right. Here's what I'd do if I wanted an array of the numbers:

 var array = [];
 yourString.replace(/~(\d+)~/g, function(_, n) { array.push(n); });

What you really don't need is that leading "^" anchor.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • I don't want an array, I just want the first one. I'll put my code up. – beatgammit Mar 26 '11 at 20:49
  • You just want the first one? But you're getting the first one already in the results you posted ... I just don't understand then. If you just want the first one, then the "g" modifier is clearly unnecessary. – Pointy Mar 26 '11 at 20:50
  • Well, with the "g" modifier, I'm getting a single string surrounded by tildes. I don't want the tildes, just the number. I suppose I could do a substring or something else to trim them off, but I'd like to find a way through regex. – beatgammit Mar 26 '11 at 20:52
  • You really should check [the documentation for the RegExp methods you want to use.](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/match) – Pointy Mar 26 '11 at 20:54
  • I understand why I'm getting an array. I've used regex in other places to split text, but why am I getting the tildes? I thought the parentheses captured only what's inside. It should match the whole string but only add the \d+ data to the result, right? – beatgammit Mar 26 '11 at 20:58
  • My apologies. I had thought that I was getting two matches (one with the delimiter and one without), but I guess the results showed the string that matched followed by the string that was captured. Thanks so much for your help! – beatgammit Mar 26 '11 at 21:09
  • 2
    The array you get has a first element `m[0]` which holds the _overall_ mattch (which includes the tildes), the next element `m[1]` contains the contents of the first capture group, which is what you are after. – ridgerunner Mar 26 '11 at 21:13
1

You only need the regex /\d+/ in order to match the first number after tilde in your example. Your method would then be:

lineReader.eachLine(file, function (line) {
    var entry = {};
    entry.id = line.match(/\d+/);
    console.log(entry);
});

With input "~05276~^~0500~^~Turkey" you will get the result "05276".

Regarding the array answer you get, it's because you have parentheses, i.e. a capture group. If it's a match the group captured, starting at the leftmost parenthesis -- (\d+) in your case -- will reside in the second slot of the result array. The first slot has the full match, which in your case includes tildes.

Staffan Nöteberg
  • 4,095
  • 1
  • 19
  • 17