简体   繁体   中英

cant understand tcl regexp pattern matching, usage of ^ and +

I have a simple TCL regexp matching,

regexp {^[^,]+} $n id

that works on strings like "1,last". it strips the text and returns only the number, but I cant understand how it works I though that the "^" sign means "ignore"/negate

I cant see how it identifies the number hope you can help me...thanks

^ can have two meanings.

Outside of a character class, it's an anchor meaning "start of string" (or start of line, depending on current options).

Within a character class , it negates its contents. [^abc] matches a character that is neither an a , a b , nor a c .

So in your example, the regex only matches from the start of the string/line, thereby allowing [^,]+ to match the 1 and preventing it from matching last .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM