How to construct this regular expression?

1

I'm trying to build a regular expression and it's a bit difficult.

I would like you to help me, and if possible explain how the computer works in relation to this expression I am asking.

I need to get everything at the beginning of a String, until a group of Strings is found, for example:

I want to get from the beginning of the String until it is found "2.8" or "6v" or "2p".

Thank you.

    
asked by anonymous 30.12.2016 / 22:59

2 answers

5

/^(.)+(?=([\d+]\.?[\d+]|\d\w))/g

^(.)+
(?=
    (
        [\d+]\.?[\d+]
        |
        \d\w
    )
)

This expression will get any text that comes before "0.0" or "0x".

Explanation of the expression:

  • ^(.)+ any text since the beginning of the string
  • (?=) text must be followed by the following expression:
  • [\d+]\.?[\d+] digit point digit, or ...
  • \d\w digit and one letter
30.12.2016 / 23:20
0
/((^[\s\S]+)2\.8)|((^[\s\S]+)6v)|((^[\s\S]+)2p)/g

Explaining:

[\s\S] takes everything.

+ takes all the characters that satisfy the previous condition, therefore "everything of everything".

^ indicates the beginning of the String, so I put it before fetching "all".

After that, just place where you want the search to stop. The parentheses are for organization purposes, to leave everything apart.

| is an "or" operator. I just put the same code 3 times, changing only the stop condition.

\. is to search for the point. I had forgotten to add but I edited.

Here's an example: link

    
31.12.2016 / 00:43