Javascript RegExp find with condition but without showing them - regex

I'm trying to find the words between the brackets.
var str = "asdfasdfkjh {{word1}} asdf fff fffff {{word2}} asdfasdf";
var pattern = /{{\w*}}/g;
var str.match(pattern); // ["{{word1}}","{{word2}}"]
This closes the deal, but gives it with the brackets, and i don't want them.
Sure, if I used the native replace on the results i could remove them. But i want the regexp to do the same.
I've also tried:
var pattern = /(?:{{)(\w*)(?:}})/g
but i can't find the real deal. Could you help me?
Edit: i might need to add a note that the words are dynamic
solution:
Bases on Tim Piezcker awnser i came with this solution:
var arr = [],
re = /{{(\w?)}}/g,item;
while (item = re.exec(s))
arr.push(item[1]);

In most regex flavors, you could use lookaround assertions:
(?<={{)\w*(?=}})
Unfortunately, JavaScript doesn't support lookbehind assertions, so you can't use them.
But the regex you proposed can be used by accessing the first capturing group:
var pattern = /{{(\w*)}}/g;
var match = pattern.exec(subject);
if (match != null) {
result = match[1];
}

A quick and dirty solution would be /[^{]+(?=\}\})/, but it will cause a bit of a mess if the leading braces are omitted, and will also match {word1}}. If I remember correctly, JavaScript does not support look-behind, which is a bit of a shame in this case.

Related

Building a Regex String - Any assistance provided

Im very new to REGEX, I understand its purpose, but Im struggling to yet fully comprehend how to use it. Im trying to build a REGEX string to pull the A8OP2B out from the following (or whatever gets dumped in that 5th group).
{"RfReceived":{"Sync":9480,"Low":310,"High":950,"Data":"A8OP2B","RfKey":"None"}}
The other items in above line, will change in character length, so I cannot say the 51st to the 56th character. It will always be the 5th group in quotation marks though that I want to pull out.
Ive tried building various regex strings up, but its still mostly a foreign language to me and I still have much reading to do on it.
Could anyone provide me a working example with the above, so I can reverse engineer and understand better?
Thanks
Demo 1: Reference the JSON to a var, then use either dot or bracket notation.
Demo 2: Using RegEx is not recommended, but here's one in JavaScript:
/\b(\w{6})(?=","RfKey":)/g
First Match
non-consuming match: :"A
meta border: \b: A non-word=:, any char=", and a word=A
consuming match: A8OP2B
begin capture: (, Any word =\w, 6 times={6}
end capture: )
non-consuming match: ","RfKey":
Look ahead: (?= for: ","RfKey": )
Demo 1
var obj = {"RfReceived":{"Sync":9480,"Low":310,"High":950,"Data":"A8OP2B","RfKey":"None"}};
var dataDot = obj.RfReceived.Data;
var dataBracket = obj['RfReceived']['Data'];
console.log(dataDot);
console.log(dataBracket)
Demo 2
Note: This is consuming a string of 3 consecutive patterns. 3 matches are expected.
var rgx = /\b(\w{6})(?=","RfKey":)/g;
var str = `{"RfReceived":{"Sync":9480,"Low":310,"High":950,"Data":"A8OP2B","RfKey":"None"}},{"RfReceived":{"Sync":8080,"Low":102,"High":1200,"Data":"PFN07U","RfKey":"None"}},{"RfReceived":{"Sync":7580,"Low":471,"High":360,"Data":"XU89OM","RfKey":"None"}}`;
var res = str.match(rgx);
console.log(res);

RegEx: capture all the phrases with hyphen in them

I have a very long list of words that after converting from another format, some of the words in it are hyphenated. for example:
book, im-moral, law
intesti-nal, lung
flimflam*, fly-by-night*, illegal,
How can I capture all the phrases that have hyphen in them? In case of above example it would be:
im-moral
intesti-nal
fly-by-night
RegEx flavor: regular expressions engine implemented in EditPad Pro 7
Please take a look at this plunker link. As anubhava mentioned, we can use the same regexp. I have also added a simple example to check it.
`
var str = 'book, im-moral,law,intesti-nal,lung, flimflam*, fly-by-night*, illegal';
var re = /([a-zA-Z]+(-[a-zA-Z]+)+)/gi;
var found = str.match(re);
alert(found)
`

Regular Expression for grok matching pattern

I need the same Regex for below two lines.
PieceID = NEW00000009
piece_id="NEW00000009"
find my pattern:
[pP]iece[_]*[iI][dD][" "]*=[" "]*["]*(?<PieceID>[A-Z0-9]["]*{11})"
You are not far from the good pattern, but you need several clarifications:
To make something optional use ? (that means {0,1}) instead of * (that means {0,}).
you don't need to put quotes around a space, the space character has nothing special. Even a character were special, you never need to protect it with quotes.
Your pattern can be written like this:
[pP]iece_?[iI][dD] *= *"?(?<PieceID>[A-Z0-9]{11})"?
You can be a little more rigorous, avoiding to match something like piece_id=NEW00000009":
[pP]iece_?[iI][dD] *= *("?)(?<PieceID>[A-Z0-9]{11})\\1
The optional quote is captured in group 1 and \\1 is a back-reference for the group 1
To finish you can make it more flexible if you replace spaces with \\s* (\\s is a character class for any white-spaces) or [^\\S\\r\\n]* that allows only horizontal white-spaces.
You'll need to worry about case-sensitivity with this one, but I think it will do what you're trying to do.
var s1 = "PieceID = NEW00000009";
var s2 = #"piece_id=""NEW00000009""";
var re = new Regex(#"^(PieceID|piece_id)\s*=\s*\""?(?<PieceID>[A-Z0-9]{11})\""?$");
Console.WriteLine(re.IsMatch(s1));
Console.WriteLine(re.IsMatch(s2));
Output:
True
True

Parsing of a string with the length specified within the string

Example data:
029Extract this specific string. Do not capture anything else.
In the example above, I would like to capture the first n characters immediately after the 3 digit entry which defines the value of n. I.E. the 29 characters "Extract this specific string."
I can do this within a loop, but it is slow. I would like (if it is possible) to achieve this with a single regex statement instead, using some kind of backreference. Something like:
(\d{3})(.{\1})
With perl, you can do:
my $str = '029Extract this specific string. Do not capture anything else.';
$str =~ s/^(\d+)(.*)$/substr($2,0,$1)/e;
say $str;
output:
Extract this specific string.
You can not do it with single regex, while you can use knowledge where regex stop processing to use substr. For example in JavaScript you can do something like this http://jsfiddle.net/75Tm5/
var input = "blahblah 011I want this, and 029Extract this specific string. Do not capture anything else.";
var regex = /(\d{3})/g;
var matches;
while ((matches = regex.exec(input)) != null) {
alert(input.substr(regex.lastIndex, matches[0]));
}
This will returns both lines:
I want this
Extract this specific string.
Depending on what you really want, you can modify Regex to match only numbers starting from line beginning, match only first match etc
Are you sure you need a regex?
From https://stackoverflow.com/tags/regex/info:
Fools Rush in Where Angels Fear to Tread
The tremendous power and expressivity of modern regular expressions
can seduce the gullible — or the foolhardy — into trying to use
regular expressions on every string-related task they come across.
This is a bad idea in general, ...
Here's a Python three-liner:
foo = "029Extract this specific string. Do not capture anything else."
substr_len = int(foo[:3])
print foo[3:substr_len+3]
And here's a PHP three-liner:
$foo = "029Extract this specific string. Do not capture anything else.";
$substr_len = (int) substr($foo,0,3);
echo substr($foo,3,substr_len+3);

How to write regex to match only one digit at end of pattern?

My field is supposed to be in the format of A111-1A1, but my regex allows the very last number to be more than one digit (eg. A111-1A1212341). How do I fix this?
Below is the regex I am currently using.
var validchar = /^[A-Z](([0-9]{3})+\-)[0-9][A-Z][0-9]+$/;
Remove the + at the end of your pattern. That is what allows for more than one numeric at the end.
var validchar = /^A-Z[0-9][A-Z][0-9]$/;
However, your pattern otherwise doesn't look right to do what you say you want. Is that really the exact pattern you are using?
Try this
var validchar = /^[A-Z][0-9]{3}\-[0-9][A-Z][0-9]$/;
Or remove the + from the end of your regex
var validchar = /^A-Z[0-9][A-Z][0-9]$/;
Just remove the final + from your regex:
var validchar = /^[A-Z]([0-9]{3})+\-[0-9][A-Z][0-9]$/;