简体   繁体   中英

Regex may contain a character

I have 2 kinds of data.

One data is something like this

ws=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9

and the another data like this

ws=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9;utmz=111872281.1437151704.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);

I wanna get the ws value, like

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9

But i got problems for split the ";" character. Because sometime the data have the character, but sometimes it doesn't have that character.

I already tried using

ws=([^]*);?

and

ws=([^]*)[;?]

and I still doesn't get the correct data. Thank you very much.

You can use:

/\bws=([^;]*)/

and grab captured group #1

RegEx Demo

([^;]*) will match 0 or more of any character that is not ;

I would use this:

ws=(.*?)(;|$)

Try it online: http://regexr.com/3bfbv

  • ws= searches for that exact text
  • ( starts the matching group
  • .*? searches for any characters, but as few characters as possbile ("non-greedy")
  • ) stops the matching group
  • (;|$) searches for either a ; or the end of the text (or the line)

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