You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- 💠 [Creating a Custom Pattern](#creating-a-custom-pattern)
@@ -30,7 +40,7 @@ Feeling overwhelmed by the documentation? You can streamline your experience by
30
40
- 💠 [Custom Character Sets and Groups](#to-custom-character-sets-and-groups)
31
41
- 💠 [Quantifier Values](#quantifier-values)
32
42
-**[Advanced usage](#advanced-usage)**
33
-
- ⚙️[Options](#options)
43
+
- ⚙️[Options](#options%EF%B8%8F)
34
44
- 💠 [Options as extra assertions](#options-as-extra-assertions)
35
45
- 💠 [Options as filters](#options-as-filters)
36
46
- 💠 [Options list](#options-list)
@@ -90,10 +100,11 @@ EloquentRegex::start("#hello #world This is a #test")->hash()->text()->get();
90
100
91
101
## Key Features🔑
92
102
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.
94
104
-**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.
97
108
98
109
_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._
99
110
@@ -111,7 +122,7 @@ Need to get started quickly? Read the [quick start guide](https://medium.com/@re
111
122
112
123
# Basic Usage
113
124
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**.
115
126
116
127
First of all, you need to include EloquentRegex class.
117
128
@@ -125,7 +136,286 @@ use Maestroerror\EloquentRegex\EloquentRegex;
125
136
use Maestroerror\EloquentRegex\Facades\EloquentRegex;
126
137
```
127
138
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.
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.
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.
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)
->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:
129
419
130
420
```
131
421
[Initiator][Pattern][?Optional][Action]
@@ -139,15 +429,15 @@ Let's break it down:
139
429
EloquentRegex::source($yourString);
140
430
```
141
431
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:
143
433
144
434
```php
145
435
EloquentRegex::source($yourString)->url();
146
436
```
147
437
148
438
_Note: **?Optional** methods mostly are the expression flags, we will talk about them in next sections_
149
439
150
-
-**_Action_** is the execution method, checkthe example:
440
+
-**_Action_** is the execution methods like `get`, `check` and etc. Check the examples:
151
441
152
442
```php
153
443
// 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
288
578
289
579
## Custom Patterns🛠️
290
580
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:
292
582
293
583
```php
294
584
EloquentRegex::start($yourString);
@@ -1183,9 +1473,9 @@ To stay updated, follow the GitHub repository for the latest changes, releases,
1183
1473
- Fix traits URLs in docs ✅
1184
1474
- Add replace method ✅
1185
1475
- 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]” ✅
1189
1479
- Add options for new patterns:
1190
1480
- Add `contains` and `notContains` options
1191
1481
- 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,
1195
1485
- Write documentation:
1196
1486
1197
1487
- 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 ✅
1208
1489
1209
1490
- Add builderPattern methods list MD file and link from the Docs.
0 commit comments