3

Is it possible to use regex to search from end of string to beginning?

for example i have a string: "a2bba2b"

I know i need the last occurrence of /a2/ and it seems highly inefficient to make machine go trough whole string which can be quite long. I know the needle will be somewhere near the end of string(haystack), so i would like to reverse the order simply tell the script to search from end to beginning to save some resources. Regex seems like a way to go match more complicated string.And it is way easier to write complex regex than your own search function.

So is there a way maybe a flag or something to reverse searching order to match regex from end to start of input string. Does any of the modern languages support something like that? if not why?

Vjerci
  • 528
  • 2
  • 6
  • 16
  • What do you mean by "i need the last occurrence of /a2/"? What is expected valid and invalid match? – guest271314 Jun 09 '17 at 01:15
  • @guest271314 smth like .lastIndexOf() but written in regex executing regex method search() – Vjerci Jun 09 '17 at 01:18
  • Why is `RegExp` necessary if `.lastIndexOf()` returns expected result? What are you trying to achieve? – guest271314 Jun 09 '17 at 01:19
  • @guest271314 save machine time by making it search from end since string to search is big and in most cases the needle is somewhere near end – Vjerci Jun 09 '17 at 01:20
  • Search for what? To return a `Boolean`; or, an index reflecting the position within the input string if the specific characters are matched? How do you know that searching from the opposite direction will "save machine time"? – guest271314 Jun 09 '17 at 01:22
  • to return a Boolean – Vjerci Jun 09 '17 at 01:23
  • 1
    You could convert string to an array, then use `Array.prototype.reduceRight()` or `.reverse()` to reverse string, though then we have introduced further function calls. What exactly is not returning expected result using current approach? It is not clear what issue is? – guest271314 Jun 09 '17 at 01:25
  • _"Questions seeking debugging help (**"why isn't this code working?"**) must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it **in the question itself**. Questions without **a clear problem statement** are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve)."_ – guest271314 Jun 09 '17 at 01:31
  • tbh i am testing atm because i had no idea lastIndexOf takes regex as input. will post an answer if i find one – Vjerci Jun 09 '17 at 01:35
  • the issue is that input string that needs to be searched is very large and speed is crucial, so when i use regex to search, in most cases it could go fast if it could search from end to beginning.Because in most cases what i am searching for is close to the end of input string. Your proposed solution reverse is highly inefficient on big input string, not sure about reduceRight i am still testing that if i could get either boolean that says it matches or index of string that matches that would be great. – Vjerci Jun 09 '17 at 01:41
  • _"speed is crucial"_ , _"go fast"_ , _"highly inefficient"_ What is benchmark for current approach? _"highly inefficient"_ compared to? Have you read previous comment? – guest271314 Jun 09 '17 at 01:43
  • i did ok lets say the input string is big 1 Terabayte and you know in most cases the needle is somewhere in last megabayte. – Vjerci Jun 09 '17 at 01:47
  • _"lets say"_ Is that a hypothetical scenario which cannot be practically reproduced by viewers of Question? You could read the data from reverse using `ReadableStream` - by first expending the resources to reverse a terabyte of data - though we still need to know what the current benchmark is, or else the inquiry truly is _"the needle is somewhere"_ – guest271314 Jun 09 '17 at 01:51
  • look tbh it is the same thing if string is 20KB which is the case and you need to find match in 10ms on bad machine and you know it is somwhere near the end... see no reason for tensions – Vjerci Jun 09 '17 at 01:54
  • There are no "tensions", at least none here. This is direct communication. What is "bad machine"? – guest271314 Jun 09 '17 at 01:57
  • a win 10 4gb ram with 10 chrome tabs opened and some duo cores 4 years old processor, could as well be smth like first gen raspberry. How does that contribute to solution? – Vjerci Jun 09 '17 at 02:02
  • Neither contribute to solution because no clear problem statement appears at original Question, thus no solution is possibly viable at this point. – guest271314 Jun 09 '17 at 02:04
  • Yesterday would have simply voted to close the Question due to being unclear or lacking clear problem statement. However, that procedure has its own lack of clarity. More time consuming to engage in an apparently chase of the wild goose, though perhaps you have performed benchmarks, and we will be able to reproduce them. – guest271314 Jun 09 '17 at 02:06
  • seems pretty clear to me. a solid answer is no node doesn't support that thing "go" does or that is not possible because of inner workings of regex. there are many solid answers – Vjerci Jun 09 '17 at 02:07
  • Then you should post Answer to your own Question https://stackoverflow.com/help/self-answer – guest271314 Jun 09 '17 at 02:08
  • 1
    i will if nobody responds in a day. i do it most of the time – Vjerci Jun 09 '17 at 02:09
  • I am still hoping this is not becoming a node or regex feature request – Vjerci Jun 09 '17 at 02:10
  • ps. seems quite obvious to me that there is no need for benchmarks, its just pure inefficiency in edge cases. but to implement smth like that to support regex with reverse search would require a lot of work, so for concrete uses atm it seems the most efficient way to implement costume search algoritham written in js and ignore regex. – Vjerci Jun 09 '17 at 02:16
  • 1
    You found your needle – guest271314 Jun 09 '17 at 02:18
  • 1
    I found an inefficient haystack too , god lately it seems to me all i want to do is submit feature requests at js fucking v8 not supporting me the way i want it, gotta submit some pull request when i get time – Vjerci Jun 09 '17 at 03:31

2 Answers2

1

No, JavaScript RegExp does not allow searching from the end of the string. The usual workaround is using a greedy dot matching pattern to make the engine grab the whole string and then backtrack to find the last occurrence (as /.*a\d/), but it will become a pain if a large string does not really contain this pattern.

If the purpose is to extract the last match, you may get all matches and grab the last one (s.match(/a\d/g).pop() (well, there must be a null check, of course)).

There are regex engines that actually let you parse the string from right to left.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

How about string reverse your string and your regex? then get the first occurence?

perseusl
  • 348
  • 1
  • 14