Regex pattern

Post your questions and help other users.

Moderator: Martin

Post Reply
akhileshg1988
Posts: 109
Joined: 16 Apr 2014 04:57

Regex pattern

Post by akhileshg1988 » 27 Aug 2017 07:06

Hi
Suppose I have the following string:
"There were more than 1000 people present at the place where the beautiful actress' visit was scheduled, though many had left the spot by the time the actress arrived."
Out of this entire text, how can I extract a part of it?
Say, how can I retrieve the text from 'more' to 'left' i.e. "more than 1000 people present at the place where the beautiful actress' visit was scheduled, though many had left"?
Can it be accomplished using the function findAll? Also guide me regarding the regex pattern that'll be helpful in this situation.

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Regex pattern

Post by Desmanto » 27 Aug 2017 08:16

The easiest way to start learn it is by using it. And regex usage typically differ for each case, so need a lot of try and error.
Fortunately, automagic has regex tester built-in (in the script action), which make testing much fun.

Just paste your whole text at the sample text.
There were more than 1000 people present at the place where the beautiful actress' visit was scheduled, though many had left the spot by the time the actress arrived.

Then copy the text you wanna extract at the Regular expression.
more than 1000 people present at the place where the beautiful actress' visit was scheduled, though many had left

You should see the green color indicating the match.
The result below will show what happen if you use certain function to match them, green text is the exact automagic regex pattern.
Pay attention to the escaped character (there are single quote at the sample text).

Then you can start to simplify the regex. Because the text is too wide and most likely can't match exactly like that.
You need to see the unique keyword which always appear and can be used to marking the beginning and ending.

For example you have exactly 'more' as start, 'left' as end. You can compress the whole regex into

Code: Select all

more.*left
. means any character except newline.
* is quantifier, to signify how many . to find, where * means zero or more.
When this 2 combined, means find zero or more of any character

This regex will match :
moreleft
more to left
more than 1000 people left

For more information about the pattern you can use, check out automagic regex pattern
http://automagic4android.com/automagic/ ... terns.html

Regex usage varies depend on case. It it better to be more specific, so we can try the regex and check if it works.
There are other thread where we use regex to parse quite specific data, but probably quite confusing if you look at it now.
http://automagic4android.com/forum/view ... f=5&t=6878
So it is better to start first from the things we need.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Post Reply