How to select this piece of text in Regex

Asked

Viewed 2,728 times

3

I have the following text example:

1.1.1.1.1. Test. Test 1.

1.1.1.1. Testing

1. Testing

I would like to select the 1. of all but until the test. I used the following regex: ([0-9]. ) the problem is that in the first line it selects the 1. in the Teste1. and I would like you to select until you start the first letter. Could you help me with this?

Thank you in advance!

EDIT 1:

Selecting everything in front of the last 1. also helps. These numbers are the levels of a tree and would like to remove only them keeping the rest of the text

  • 1

    Using some programming language or just a text editor?

  • I’m using Delphi, but in this case it’s only for text even by the Regex of Delphi

  • @Matheusmachado rray question is why every regex engine has its peculiarities

3 answers

4

If I understand correctly you want to filter a part of the content and want to replace it.

So even if there is something else you want it will always be something like :

1.1.1.1. Testing.
1.1. Testing.
1. Testing.

Still commented on tree, in this case there may be other indices as :

1.43.7.112. Test.
26.0.177. Test.

But you want to remove the contents by keeping only the text.

Upshot

pattern : /(\d+\.)+ (\w)/g
replace : $2

Explanation

  • (\d+\.)+ - This part takes care of the indexes, as is a group with +, it will repeated the possibility of the group as much as it can, being at least 1.
  • - Literal space (I believe after the index you have a spacing)
  • (\w) - Group 2, REGEX limit to know when the text started.

The replace replaces everything that was captured.

  • $2 - replaces the entire sentence with the character captured in group 2.

See working in REGEX101.

4


Dude, run through the lines and apply:

^\s*[0-9\.]*\s*

This regex removes what you want... only it has to be rotated line by line, with a FOR or WHILE and giving a replace of the value found by ''.

If you prefer, you can use the regex below and replace it with a line break ( n):

(^|\n)\s*[0-9\.]*\s*
  • I don’t understand, I could explain better the solution?

  • I updated the answer

  • Thank you so much! That’s exactly what I wanted "^\s[0-9.]\s*"**

3

I recommend making regular expressions always gradually, by parts. Look how I built a expression to validate emails.

Come on, get the more general:

.*

Here we marry everything, so let’s filter. It is necessary to pick from the beginning, so we can put the anchor ^ soon:

^.*

Hmmm, you also said you want everything until you start the first letter... so we can marry anything but letters, how about? We’ll use the denied list for that:

^[^A-Za-z]*

Well, let’s just make sure it goes to one letter? We will use the list for this, and we can also group for a possible text substitution in the future:

 ^([^A-Za-z]*)[A-Za-z]

Ok, now we have the desired text in the grouping recognized by the rear view mirror \1 =D

More about regex see Quick Regular Expression Query Guide of Aurelio Verde.

  • Who voted negative could justify what I answered wrong?

  • 1

    I do not know why they voted negative, but although not what I wanted it helped me to think better the next time I need to use regex, thank you!

  • @Matheusmachado, I was happy and sad for your answer. Happy to have helped you think better in regex, sad not to answer what you wanted. I will try to improve on the next =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.