Skip to content

Commit 34961af

Browse files
committed
Updated docs with actions
1 parent 7508410 commit 34961af

File tree

1 file changed

+303
-22
lines changed

1 file changed

+303
-22
lines changed

README.md

Lines changed: 303 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ Feeling overwhelmed by the documentation? You can streamline your experience by
1818
- 🔑[Key Features](#key-features)
1919
- 🧭[Getting Started](#getting-started)
2020
- **[Basic Usage](#basic-usage)**
21+
-[Actions](#actions)
22+
- [Get](#get)
23+
- [Check](#check)
24+
- [CheckString](#checkstring)
25+
- [Count](#count)
26+
- [Replace](#replace)
27+
- [ToRegex](#get-regex)
28+
- [Search](#search)
29+
- [SearchReverse](#searchreverse)
30+
- [Swap](#Swap)
2131
- 📑[Ready-to-Use Patterns](#ready-to-use-patterns)
2232
- 🛠️[Custom Patterns](#custom-patterns)
2333
- 💠 [Creating a Custom Pattern](#creating-a-custom-pattern)
@@ -30,7 +40,7 @@ Feeling overwhelmed by the documentation? You can streamline your experience by
3040
- 💠 [Custom Character Sets and Groups](#to-custom-character-sets-and-groups)
3141
- 💠 [Quantifier Values](#quantifier-values)
3242
- **[Advanced usage](#advanced-usage)**
33-
- ⚙️[Options](#options)
43+
- ⚙️[Options](#options%EF%B8%8F)
3444
- 💠 [Options as extra assertions](#options-as-extra-assertions)
3545
- 💠 [Options as filters](#options-as-filters)
3646
- 💠 [Options list](#options-list)
@@ -90,10 +100,11 @@ EloquentRegex::start("#hello #world This is a #test")->hash()->text()->get();
90100

91101
## Key Features🔑
92102

93-
- **Ready-to-Use Patterns**: Common patterns like emails, URLs, and IP addresses are pre-defined and ready to go. Just a few keystrokes and you're validating.
103+
- **Ready-to-Use Patterns**: Common patterns like emails, URLs, IP addresses and etc. are pre-defined and ready to go. Just a few keystrokes and you're validating.
94104
- **Custom Patterns Made Easy**: Build your own regex patterns with an easy-to-use, fluent interface. Say hello to readable regex!
95-
- **Options and Filters**: Tailor your regex operations with options and filters for precision matching. It's like having a regex wizard at your fingertips.
96-
- **Laravel Integration**: Seamlessly integrates with your Laravel projects, leveraging Laravel's elegant syntax and features.
105+
- **Useful actions**: You can perform various actions with your pattern, from simply validating and getting the matches to complex actions like `search` or `replace`.
106+
- **Options and Filters**: Tailor your regex operations with options and filters for precision matching.
107+
- **Laravel Integration**: Seamlessly integrates with your Laravel projects, leveraging Laravel's elegant syntax and features like collection.
97108

98109
_For more details about package and it's inner workings check out [STRUCTURE.md](https://github.com/MaestroError/eloquent-regex/blob/update-documentation-and-add-advanced-usage-section/STRUCTURE.md) file._
99110

@@ -111,7 +122,7 @@ Need to get started quickly? Read the [quick start guide](https://medium.com/@re
111122

112123
# Basic Usage
113124

114-
EloquentRegex simplifies regular expressions in Laravel, making it easy to validate data, search text, and extract information. This section introduces the basic usage of EloquentRegex, including leveraging ready-to-use patterns and creating custom patterns.
125+
EloquentRegex simplifies regular expressions in Laravel, making it easy to validate data, search text, and extract information. This section introduces the basic usage of EloquentRegex, including leveraging **ready-to-use patterns** and creating **custom patterns**.
115126

116127
First of all, you need to include EloquentRegex class.
117128

@@ -125,7 +136,286 @@ use Maestroerror\EloquentRegex\EloquentRegex;
125136
use Maestroerror\EloquentRegex\Facades\EloquentRegex;
126137
```
127138

128-
Usage structure is very similar to Laravel's Eloquent ORM, check this out:
139+
## Actions
140+
141+
Actions are end methods created to finilize your pattern and take some action with it. So they are the main features of the package as well. Let's discuss them one by one and check the examples for [custom](#custom-patterns%EF%B8%8F) and [ready-to-use](#ready-to-use-patterns) patterns.
142+
143+
### Get
144+
145+
Returns all matches as array/collection.
146+
147+
_Example with ready-to-use pattern_
148+
149+
```php
150+
EloquentRegex::source("Support: [email protected]; Info: [email protected]")
151+
->email()
152+
->get();
153+
154+
```
155+
156+
_Example with custom pattern_
157+
158+
```php
159+
EloquentRegex::start("#hello #world This is a #test")
160+
->hash()->text()
161+
->get();
162+
// Returns: ['#hello', '#world', '#test']
163+
```
164+
165+
### Check
166+
167+
Checks if string exactly matches the pattern from start to end (strict match).
168+
169+
_Example with ready-to-use pattern_
170+
171+
```php
172+
EloquentRegex::source("[email protected]")
173+
->email()->check();
174+
// Returns: true
175+
```
176+
177+
_Example with custom pattern_
178+
179+
```php
180+
EloquentRegex::start("#test")
181+
->hash()->text()
182+
->check();
183+
// Returns: true
184+
```
185+
186+
### CheckString
187+
188+
Checks if string contains any matches of pattern. In case of email pattern, it will return `true` if one or more email is present in the given source string.
189+
190+
_Example with ready-to-use pattern_
191+
192+
```php
193+
EloquentRegex::source("Support: [email protected]; Info: [email protected]")
194+
->email()->checkString();
195+
// Returns: true
196+
```
197+
198+
_Example with custom pattern_
199+
200+
```php
201+
EloquentRegex::start("#hello #world This is a #test")
202+
->hash()->text()
203+
->checkString();
204+
// Returns: true
205+
```
206+
207+
### Count
208+
209+
Counts amount of matches and returns as int. Returns `0` if no matches found.
210+
211+
_Example with ready-to-use pattern_
212+
213+
```php
214+
EloquentRegex::source("Support: [email protected]; Info: [email protected]")
215+
->email()->count();
216+
// Returns: 2
217+
```
218+
219+
_Example with custom pattern_
220+
221+
```php
222+
EloquentRegex::start("#hello #world This is a #test")
223+
->hash()->text()
224+
->count();
225+
// Returns: 3
226+
```
227+
228+
### Replace
229+
230+
Counts amount of matches and returns as int. Returns `0` if no matches found.
231+
232+
_Example with ready-to-use pattern_
233+
234+
```php
235+
EloquentRegex::source("Support: [email protected]; Info: [email protected]")
236+
->email()
237+
->replace(function($foundItem) {
238+
return "<span>" . $foundItem . "</span>";
239+
});
240+
// Returns: "Support: <span>[email protected]</span>; Info: <span>[email protected]</span>"
241+
```
242+
243+
_Example with custom pattern_
244+
245+
```php
246+
EloquentRegex::start("This is a #test")
247+
->hash()->text()
248+
->replace(function($foundItem) {
249+
return "<a href='$foundItem'>" . $foundItem . "</a>";
250+
});
251+
// Returns: "This is a <a href='#test'>#test</a>"
252+
```
253+
254+
### ToRegex
255+
256+
Returns built raw regex as string. If any [options](#options%EF%B8%8F) applied, it will **not be returned** using `toRegex` method.
257+
258+
_Example with custom pattern_
259+
260+
```php
261+
EloquentRegex::builder()->start()
262+
->textLowercase()
263+
->atSymbol()
264+
->textLowercase()
265+
->dot()
266+
->textLowercaseRange(2, 4)
267+
->toRegex();
268+
// Returns: "[a-z]+@[a-z]+\.[a-z]{2,4}"
269+
```
270+
271+
### Search
272+
273+
Search method searches for **keyword** or **pattern** in multiline text and returns lines where subject is found. It is especially useful with processing of large files like logs or JSON.
274+
275+
_Example with keyword search_
276+
277+
```php
278+
EloquentRegex::source(
279+
"
280+
Whose woods these are I think I know.\n
281+
His house is in the village though;\n
282+
He will not see me stopping here\n
283+
To watch his woods fill up with snow.\n
284+
\n
285+
The woods are lovely, dark and deep,\n
286+
But I have promises to keep,\n
287+
And miles to go before I sleep,\n
288+
And miles to go before I sleep.\n
289+
"
290+
)
291+
->search("woods");
292+
/* Returns: [
293+
"Whose woods these are I think I know.",
294+
"To watch his woods fill up with snow.",
295+
"The woods are lovely, dark and deep,",
296+
]
297+
*/
298+
```
299+
300+
_Example with pattern_
301+
302+
```php
303+
EloquentRegex::source(
304+
"
305+
Please contact us via email at [email protected] for more details.
306+
For support inquiries, you can also email us at [email protected].
307+
Our marketing team is reachable at [email protected] for collaborations.
308+
For urgent matters, you can reach out through the phone number provided.
309+
Subscribe to our newsletter to stay updated with the latest news.
310+
Feel free to send feedback directly to our office address.
311+
Any emails sent after 5 PM may be responded to the next business day.
312+
Check the FAQ section for answers to common questions.
313+
Social media channels are also available for quick updates.
314+
We value your input and encourage you to share your thoughts.
315+
"
316+
)
317+
->search(function ($pattern) {
318+
$pattern->email();
319+
});
320+
/* Returns:
321+
[
322+
'Please contact us via email at [email protected] for more details.',
323+
'For support inquiries, you can also email us at [email protected].',
324+
'Our marketing team is reachable at [email protected] for collaborations.'
325+
]
326+
*/
327+
```
328+
329+
### SearchReverse
330+
331+
SearchReverse method searches for **keyword** or **pattern** in multiline text and returns every line which doesn't contains subject. It is especially useful while processing of large files like logs or JSON.
332+
333+
_Example with keyword search_
334+
335+
```php
336+
// Find all logs types except INFO
337+
EloquentRegex::source(
338+
"
339+
[2024-12-23 10:00:00] INFO: User logged in.\n
340+
[2024-12-25 10:05:00] ERROR: Unable to connect to database.\n
341+
[2024-12-25 10:10:00] INFO: User updated profile.\n
342+
[2024-12-15 10:15:00] WARNING: Disk space running low.\n
343+
[2024-12-34 10:20:00] ERROR: Timeout while fetching data.\n
344+
"
345+
)
346+
->searchReverse("INFO");
347+
/* Returns: [
348+
'[2024-12-25 10:05:00] ERROR: Unable to connect to database.',
349+
'[2024-12-15 10:15:00] WARNING: Disk space running low.',
350+
'[2024-12-34 10:20:00] ERROR: Timeout while fetching data.',
351+
]
352+
*/
353+
```
354+
355+
### Swap
356+
357+
Swap method allows you to swap any kind of data logically, for example, build new URLs from old ones. It utilizes "named groups" regex feature and can be used with **callback** or **pattern string** (Check the example)
358+
359+
_Example with pattern string_
360+
361+
```php
362+
$builder= EloquentRegex::start("URIs: /container-tbilisi-1585, /container-berlin-1234, /container-tbilisi-2555")
363+
->slash() // "/"
364+
->exact("container") // "container" (static part of URI)
365+
->dash() // "-"
366+
->namedGroup(function ($pattern) {
367+
return $pattern->text();
368+
}, "City") // Text between dashes, Grouped & named as "city"
369+
->dash() // "-"
370+
->namedGroup(function ($pattern) {
371+
return $pattern->digitsRange(2, 5);
372+
}, "id") // Numbers at end, Grouped & named as "id"
373+
->end(); // Ends custom pattern to make "swap" method available
374+
375+
// Using swap with pattern string
376+
// which will swap placeholders like "[ID]" with
377+
// Extracted data for each found match
378+
$builder->swap("/container/[ID]?city=[CITY]");
379+
380+
/* Returns:
381+
[
382+
'/container/1585?city=tbilisi',
383+
'/container/1234?city=berlin',
384+
'/container/2555?city=tbilisi'
385+
]
386+
*/
387+
```
388+
389+
_Example with callback_
390+
391+
```php
392+
$builder = EloquentRegex::start("Issues in progress: RI-2142, RI-1234, PO-2555");
393+
$builder
394+
->namedGroup(function ($pattern) {
395+
return $pattern->textUppercase(2);
396+
}, "project", 1) // 2 uppercase char named as "project"
397+
->dash() // "-"
398+
->namedGroup(function ($pattern) {
399+
return $pattern->digitsRange(2, 4);
400+
}, "issue", 1) // from 2 to 4 digits named as issue
401+
->end();
402+
403+
$results = $result->swap(function ($data) {
404+
return "The issue #" . $data["issue"] . " of project " . $data["project"] ." is in progress";
405+
});
406+
407+
/* Returns:
408+
[
409+
'The issue #2142 of project RI is in progress',
410+
'The issue #1234 of project RI is in progress',
411+
'The issue #2555 of project PO is in progress'
412+
]
413+
*/
414+
```
415+
416+
## Usage structure
417+
418+
As you may already found out, the usage structure is similar to Laravel's Eloquent ORM, check this out:
129419

130420
```
131421
[Initiator][Pattern][?Optional][Action]
@@ -139,15 +429,15 @@ Let's break it down:
139429
EloquentRegex::source($yourString);
140430
```
141431

142-
- **_Pattern_** Could be method for one of the ready-to-use patterns or your custom pattern (we will talk about custom patterns later). Let's keep the example simple and add url pattern:
432+
- **_Pattern_** Could be method for one of the ready-to-use patterns or your custom pattern (we will talk about custom patterns later). Let's keep the example simple and add `url` pattern:
143433

144434
```php
145435
EloquentRegex::source($yourString)->url();
146436
```
147437

148438
_Note: **?Optional** methods mostly are the expression flags, we will talk about them in next sections_
149439

150-
- **_Action_** is the execution method, check the example:
440+
- **_Action_** is the execution methods like `get`, `check` and etc. Check the examples:
151441

152442
```php
153443
// get() will return array/collection of URLs if any found in $yourString
@@ -288,7 +578,7 @@ Didn't it cover all your needs? Let's take a look to the custom patterns section
288578

289579
## Custom Patterns🛠️
290580

291-
For scenarios where predefined patterns do not suffice, EloquentRegex allows you to define custom patterns using the start or customPattern methods as initiator:
581+
For scenarios where predefined patterns do not suffice, EloquentRegex allows you to define custom patterns using the `start` or `customPattern` methods as initiator:
292582

293583
```php
294584
EloquentRegex::start($yourString);
@@ -1183,9 +1473,9 @@ To stay updated, follow the GitHub repository for the latest changes, releases,
11831473
- Fix traits URLs in docs ✅
11841474
- Add replace method ✅
11851475
- Add reverse method (to get everything except matched pattern) - try negative lookBehind or Lookahead with big texts ✅
1186-
- Search and SearchReverse with keyword or pattern, check the searchTest\search.php file
1187-
- Implement usage of named groups: `/(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})/`
1188-
- Make available to do thing like this: “/container-[MATERIAL_NAME]-[NUMBER]” → “/konfigurator/materialien?material=[MATERIAL_NAME]
1476+
- Search and SearchReverse with keyword or pattern, check the searchTest\search.php file
1477+
- Implement usage of named groups: `/(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})/`
1478+
- Make available to do thing like this: “/container-[MATERIAL_NAME]-[NUMBER]” → “/konfigurator/materialien?material=[MATERIAL_NAME]
11891479
- Add options for new patterns:
11901480
- Add `contains` and `notContains` options
11911481
- usernameLength: Set minimum and maximum length for the username part of the email.
@@ -1195,16 +1485,7 @@ To stay updated, follow the GitHub repository for the latest changes, releases,
11951485
- Write documentation:
11961486

11971487
- Create quick start guide and add in Docs. ✅
1198-
- Add in Basic Usage topic the "Features" docs with both pattern examples
1199-
1200-
- get
1201-
- check
1202-
- checkString
1203-
- count
1204-
- toRegex
1205-
- replace
1206-
- search
1207-
- searchReverse
1488+
- Add in Basic Usage topic the "Features" docs with both pattern examples ✅
12081489

12091490
- Add builderPattern methods list MD file and link from the Docs.
12101491
- Add options debuging section in docs

0 commit comments

Comments
 (0)