Get substring from an optional pattern

user3685080

I am trying to parse the following URL:

http://localhost:30001/catalog/search?tags=bed-green-big-33-22-ancient-5--2

Where:

  1. bed-green-big-33-22-ancient-5 is 1 Group (filters)
  2. --2 is Group 2 [PageNumber] and it's optional

My regex attempt is:

 tags=(.*)--(\d*)

It works as it captures exactly what I need, but it does not account for the optional --2 at the end.

Results should be: bed-green-big-33-22-ancient-5, 2.

Wiktor Stribiżew

Let's consider a simple one-regex approach.

Since your string is inside the query string, you might want to watch out for argument boundaries (& and initial ?) and use [&?] at the pattern start. Right now, .* will match everything even if you have more than 1 argument. In order to make sure you match groups separated with - but not overmatch after &, you can use a negated character class [^&-]

Next thing to consider is the optional part --<NUMBER>. You need to group the characters and apply a ? quantifier to that group to make it "optional" one time (? means match 1 or 0 times). To make our match result cleaner, it is advisable to use non-capturing groups.

So, the regex will look like:

[&?]tags=([^&-]*(?:-[^&-]+)*)(?:--(\d+))?
  ^      |     Main         ||    ^Grp2^| 
 Start   |   capture        ||          |
boundary |    group         || Optional |

See regex demo (\n is added since this is a multiline demo).

JS:

var re = /[&?]tgs=([^&\n-]*(?:-[^&\n-]+)*)(?:--(\d+))?/; 
var str = 'http://localhost:30001/catalog/search?tags=bed-green-big-33-22-ancient-5--2';
var m = str.match(re);
if (m !== null) {
    document.getElementById("r").innerHTML = "First part: <b>" + m[1] + "</b><br/>Second part: <b>" + m[2] + "</b>";
}
<div id="r"/>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get substring from string in javascript with a pattern

From Dev

In ruby, how can I get the substring from a pattern to a pattern? Also, how do I get the last such pattern?

From Dev

How to get a substring value from a varchar in oracle based on specific pattern

From Dev

Get Substring from text

From Dev

Get substring from a fact

From Dev

Get substring matching pattern with fixed length

From Dev

what pattern to get substring using regexp

From Dev

How to get substring with pattern using Java

From Dev

Get Substring from a String from __

From Dev

Remove substring/pattern word from a string

From Dev

Remove substring/pattern word from a string

From Dev

Replace repeating substring pattern from string with regex

From Dev

Get substring from string in Liquid?

From Dev

Get a substring from an XML file

From Dev

Get Substring from a String in Java

From Dev

SSIS Get Substring From Right

From Dev

Get length of Substring from string

From Dev

Get object type from optional?

From Dev

How to (fastly) get a substring according to a specific pattern in python?

From Dev

How to write regex pattern to get a substring of a string in c#

From Dev

capture group with optional substring

From Dev

Trying to extract a substring from string based on a user-defined pattern

From Dev

R extract substring from end of pattern until first occurance of character

From Dev

Extract substring from string using key pattern and delimiter using Ruby

From Dev

Filter a substring matching a pattern from an ansible variable and assign matched substring to another variable

From Dev

How to get a substring from a string of runes in golang?

From Dev

Get substring from pandas dataframe while filtering

From Dev

How to get substring from a sql table?

From Dev

Get substring from html string in javascript

Related Related

  1. 1

    Get substring from string in javascript with a pattern

  2. 2

    In ruby, how can I get the substring from a pattern to a pattern? Also, how do I get the last such pattern?

  3. 3

    How to get a substring value from a varchar in oracle based on specific pattern

  4. 4

    Get Substring from text

  5. 5

    Get substring from a fact

  6. 6

    Get substring matching pattern with fixed length

  7. 7

    what pattern to get substring using regexp

  8. 8

    How to get substring with pattern using Java

  9. 9

    Get Substring from a String from __

  10. 10

    Remove substring/pattern word from a string

  11. 11

    Remove substring/pattern word from a string

  12. 12

    Replace repeating substring pattern from string with regex

  13. 13

    Get substring from string in Liquid?

  14. 14

    Get a substring from an XML file

  15. 15

    Get Substring from a String in Java

  16. 16

    SSIS Get Substring From Right

  17. 17

    Get length of Substring from string

  18. 18

    Get object type from optional?

  19. 19

    How to (fastly) get a substring according to a specific pattern in python?

  20. 20

    How to write regex pattern to get a substring of a string in c#

  21. 21

    capture group with optional substring

  22. 22

    Trying to extract a substring from string based on a user-defined pattern

  23. 23

    R extract substring from end of pattern until first occurance of character

  24. 24

    Extract substring from string using key pattern and delimiter using Ruby

  25. 25

    Filter a substring matching a pattern from an ansible variable and assign matched substring to another variable

  26. 26

    How to get a substring from a string of runes in golang?

  27. 27

    Get substring from pandas dataframe while filtering

  28. 28

    How to get substring from a sql table?

  29. 29

    Get substring from html string in javascript

HotTag

Archive