steenbergh
steenbergh

Reputation: 1771

Regex - how to ignore strings starting with underscore

I need to test this string to match anything not starting with an underscore:

__$id0 = "foo"

My approach was to match the string starting with an underscore, then making that match into a negative look-ahead and capturing the rest.

Testing for the underscore works:

(_.*?\s) gives one hit: '__$id0'

However, making this into a negative look-ahead does not have the desired effect:

((?!_.*?\s).*?\s) results in

MATCH 1
1.  [2-7]   `$id0 `
MATCH 2
1.  [7-9]   `= `
MATCH 3
1.  [9-16]  `"lala"`

The underscores are stripped from the first string, but the string itself still is in the resultset. I want to exclude that first string completely, because it starts with _.

How can I exclude strings starting with _?

The result should be this:

MATCH 1
1.  [7-9]   `= `
MATCH 2
1.  [9-16]  `"lala"`

(Source: https://www.regex101.com/r/cC2pV7/1)

Upvotes: 3

Views: 6815

Answers (4)

AmerllicA
AmerllicA

Reputation: 32522

By using the following regex:

^(?!_).+

It matches words or phrases without an underscore at the beginning. if you need only the words use this:

^(?!_)\w+

Both work for C# and JavaScript.

Upvotes: 0

J Agustin Barrachina
J Agustin Barrachina

Reputation: 4090

I know the tag and everything appears to say regex must be used, but in case it helps someone, how about:

string_to_compare[0] =! '_'

Upvotes: 1

saluce
saluce

Reputation: 13360

Here's the trick...match what you don't want to keep, then match and capture everything else.

[\s]?_[^\s]*|\s?(.*?)\s+?

On the left side of the | pipe, you are matching anything that starts with an underscore. If it matches, it takes that side of the regex. Otherwise, attempt to match the right side of the underscore, which is captured and matches anything that doesn't start with an underscore.

This will also allow you to match when an underscore is present in the middle or at the end of a string.

https://www.regex101.com/r/cC2pV7/3

Upvotes: 3

poke
poke

Reputation: 387687

You should first check for the start of a line using ^. And then, you want to match anything that isn’t an underscore, so you use an inverted character class: [^_]:

^([^_].*?)\s*=\s*(.*?)

Working example

Upvotes: 4

Related Questions