Asked  7 Months ago    Answers:  5   Viewed   36 times

I have a string. The end is different, such as index.php?test=1&list=UL or index.php?list=UL&more=1. The one thing I'm looking for is &list=.

How can I match it, whether it's in the middle of the string or it's at the end? So far I've got [&|?]list=.*?([&|$]), but the ([&|$]) part doesn't actually work; I'm trying to use that to match either & or the end of the string, but the end of the string part doesn't work, so this pattern matches the second example but not the first.

 Answers

34

Use:

/(&|?)list=.*?(&|$)/

Note that when you use a bracket expression, every character within it (with some exceptions) is going to be interpreted literally. In other words, [&|$] matches the characters &, |, and $.

Tuesday, June 1, 2021
 
astaykov
answered 7 Months ago
29

If I understand you correctly, something like this might work:

-(d+)$

Match hyphen (-) followed by one or more digits (d+) just before the end of the string ($). Capture the digits only ( (...) ).

Saturday, May 29, 2021
 
Avicinnian
answered 7 Months ago
68

It is clear your text contains a linebreak other than LF. In .NET regex, a dot matches any char but LF (a newline char, n).

See Multiline Mode MSDN regex reference

By default, $ matches only the end of the input string. If you specify the RegexOptions.Multiline option, it matches either the newline character (n) or the end of the input string. It does not, however, match the carriage return/line feed character combination. To successfully match them, use the subexpression r?$ instead of just $.

So, use

@"^(#+).+?r?$"

The .+?r?$ will match lazily any one or more chars other than LF up to the first CR (that is optional) right before a newline.

Or just use a negated character class:

@"^(#+)[^rn]+"

The [^rn]+ will match one or more chars other than CR/LF.

Tuesday, June 15, 2021
 
Pupil
answered 6 Months ago
17

According to article 11.5.2. Regular Expressions in MySQL's documentation, you can perform selections with a regular expression with the following syntax

SELECT field FROM table WHERE field REGEX pattern

In order to match simple URLS, you may use

SELECT field FROM table
 WHERE field REGEXP "^(https?://|www\.)[.A-Za-z0-9-]+\.[a-zA-Z]{2,4}"

This will match most urls like

  • www.google.il
  • http://google.com/
  • http://ww.google.net/
  • www.google.com/index.php?test=data
  • https://yahoo.dk/as
  • http://goo.gle.com/
  • http://wt.a.x24-s.org/ye/
  • www.website.info

But not

  • htp://google.com
  • ww.google.com/
  • www-google.com
  • http://google.c
  • http://goo#.com
  • httpf://google.com
Saturday, August 7, 2021
 
air
answered 4 Months ago
air
83

The problem is that b, the word boundary anchor, only matches between alphanumeric and non-alphanumeric characters, so it won't match after a # (unless that is itself followed by an alphanumeric).

Use

b[A-G](?:m|b|#|sus|d)*(?:b|(?<=#))

No need to escape the #, either.

EDIT: Changed the regex to better reproduce the intended functionality (as I think it should be)

Currently, you're not matching some chords, though; how about

b[A-G](?:add|maj|j|m|-|b|#|sus|d|°)*(?:b|(?<=[#°-]))

That way, you can match all of these:

A7
Abm7 
A#m7sus4
A7b9#13
Amaj7#11
A#°
Abj7add13

I guess there is still room for improvement, though.

Saturday, November 13, 2021
 
sj112
answered 4 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share